Testing rspec模拟外部api
这里是TDD的新成员,哦 以下是我要测试()的内容,简而言之:Testing rspec模拟外部api,testing,rspec,mocking,twilio,Testing,Rspec,Mocking,Twilio,这里是TDD的新成员,哦 以下是我要测试()的内容,简而言之: account = Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN) resp = account.request( "/#{API_VERSION}/Accounts/#{ACCOUNT_SID}/SMS/Messages", 'POST', smsInfo ) 下面是测试代码尝试: describe Text
account = Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN)
resp = account.request(
"/#{API_VERSION}/Accounts/#{ACCOUNT_SID}/SMS/Messages",
'POST',
smsInfo
)
下面是测试代码尝试:
describe Text do
it "should call the Twilio API with credentials" do
#pending "mocking api although not passed in.."
t = mock(Twilio::RestAccount)
twapi = mock("new twapi").should_receive(:request).and_return(Net::HTTPSuccess)
t.stub(:new).and_return(twapi)
Twilio::RestAccount.should_receive(:new)
sms = Factory.create(:boring_sms)
sms.send_sms
end
end
它为nil:NilClass生成错误:未定义的方法'request'
我是否采取了正确的方法?谢谢 退房。这正是你想要用在这类事情上的东西 执行此操作时,您正在使用0个新参数存根:
t.stub(:new).and_return(twapi)
但你的测试是:
Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN)
这是新的,有2个参数
尝试:
并删除:
Twilio::RestAccount.should_receive(:new)
使用TWILIO和其他外部服务,我也考虑使用VCR。
好的一面是,你可以通过手动测试让它工作一次,它基本上是在验证你没有搞糟任何事情。不利的一面是,每当你们触摸VCR测试的代码时,你们经常不得不手工重新测试VCR测试的所有东西。需要考虑的其他事情。 自从有人问起这个问题,我就成了录像机的忠实粉丝!
Twilio::RestAccount.should_receive(:new)