這裏是TDD的新手,哦!rspec mocking external api
這就是我想要的測試(ruby library),簡言之:
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
產生錯誤:未定義的方法`要求」的零: NilClass
我正在採取正確的做法嗎?謝謝!
由於這個問題被問到,我已經成爲VCR的粉絲! –