Skip to content

Commit

Permalink
use webmock to test.
Browse files Browse the repository at this point in the history
  • Loading branch information
saberma committed Jun 30, 2013
1 parent 6b90eed commit aba4ab6
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -4,7 +4,7 @@

所有短信平台都禁止发送私人短信,并要求在短信内容末尾加上签名后缀,如【19屋】

* [推立方](http://tui3.com/) 需要审核内容格式和签名,自动在短信加上签名后缀
* [推立方](http://tui3.com/) 需要[配置内容格式和签名](http://www.tui3.com/Members/smsconfigv2/),自动在短信加上签名后缀
* [短信宝](http://www.smsbao.com/)
* [畅友网络](http://www.chanyoo.cn/)

Expand All @@ -25,6 +25,7 @@
## 使用

```ruby
# 支持 :tui3, :smsbao, chanyoo 短信接口
ChinaSMS.use :smsbao, username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD'
ChinaSMS.to '13912345678', '[Test]China SMS gem has been released.'
```
1 change: 0 additions & 1 deletion Rakefile
@@ -1,2 +1 @@
require "bundler/gem_tasks"
Bundler::GemHelper.install_tasks
1 change: 1 addition & 0 deletions china_sms.gemspec
Expand Up @@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "webmock"
end
2 changes: 1 addition & 1 deletion lib/china_sms.rb
Expand Up @@ -20,6 +20,6 @@ def use(service, options)
end

def to(receiver, content)
@service.to receiver, content
@service.to receiver, content, username: @username, password: @password
end
end
13 changes: 8 additions & 5 deletions lib/china_sms/service/tui3.rb
Expand Up @@ -3,15 +3,18 @@ module Service
# http://tui3.com/
module Tui3
extend self
def to(phone, content)
def to(phone, content, options)
url = "http://tui3.com/api/send/"
res = Net::HTTP.post_form(URI.parse(url), k: ChinaSMS.password, t: phone, c: URI::encode(content), p: 1, r: 'json')
res = Net::HTTP.post_form(URI.parse(url), k: options[:password], t: phone, c: content, p: 1, r: 'json')
result res.body
#{"err_code":2,"err_msg":"k,\u975e\u6cd5apikey:666666","server_time":"2013-06-30 20:17:36"}
end

def result(json)
json
def result(body)
{
success: body['err_code'] == 0,
code: body['err_code'],
message: body['err_msg']
}
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions spec/china_sms_spec.rb
@@ -1,20 +1,20 @@
require 'spec_helper'

describe "ChinaSMS" do
#let(:service) { :smsbao }
#jlet(:service) { :chanyoo }
let(:service) { :tui3 }
let(:username) { 'saberma' }
let(:password) { '666666' }
subject { ChinaSMS }
before { ChinaSMS.use service, username: 'saberma', password: password }
let(:phone) { '13928452841' }
let(:content) { '活动通知:深圳 Rubyist 活动时间变更到明天下午 7:00,请留意。' }
before { ChinaSMS.use service, username: username, password: password }
describe "#use" do
subject { ChinaSMS }
its(:username) { should eql "saberma"}
end
describe "#to" do
it 'should be success' do
#result = subject.to('13928452841', '深圳 Rubyist 活动时间变更到明天下午 7:00,请留意。【19屋】')
result = subject.to('13928452841', '活动通知:深圳 Rubyist 活动时间变更到明天下午 7:00,请留意。')
puts result
end
subject { ChinaSMS.to(phone, content) }
before { ChinaSMS::Service::Tui3.stub(:to).with(phone, content, username: username, password: password).and_return(success: true, code: 0) }
its([:success]) { should eql true }
its([:code]) { should eql 0 }
end
end
26 changes: 26 additions & 0 deletions spec/service/tui3_spec.rb
@@ -0,0 +1,26 @@
require 'spec_helper'

describe "Tui3" do
describe "#to" do
let(:username) { 'saberma' }
let(:password) { '666666' }
let(:phone) { '13928452841' }
let(:content) { '活动通知:深圳 Rubyist 活动时间变更到明天下午 7:00,请留意。' }
before do
stub_request(:post, "http://tui3.com/api/send/").
with(body: {k: password, t: phone, c: content, p: '1', r: 'json'}).
to_return(body: {
"err_code" => 0,
"err_msg" => "操作成功!",
"sms_count" => 1,
"tick_ids" => "20076900",
"remain_count" => 6,
"server_time" => "2013-06-30 22:05:31"
})
end
subject { ChinaSMS::Service::Tui3.to phone, content, username: username, password: password }
its([:success]) { should eql true }
its([:code]) { should eql 0 }
its([:message]) { should eql "操作成功!" }
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -1,3 +1,4 @@
require 'webmock/rspec'
require 'china_sms'

RSpec.configure do |config|
Expand Down

0 comments on commit aba4ab6

Please sign in to comment.