Take the 2-minute tour ×

New to TDD here, d'oh!

Here's what I want to test, in brief:

 account = Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN)
    resp = account.request(

And here's test code attempt:

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)


    sms = Factory.create(:boring_sms)

which generates the error: undefined method `request' for nil:NilClass

Am I taking the right approach? thanks!

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

With Twilio and other external services, I also consider using VCR. http://relishapp.com/myronmarston/vcr

The upside is that you get it working once with manual testing, and it's basically verifying you don't mess anything up. Downside is that any time you touch code tested by VCR, you often have to re-test everything manually that's tested by VCR. Something else to consider.

share|improve this answer
Since this question was asked, I've become a big fan of VCR! –  Peter Ehrlich Jan 24 '13 at 23:46
add comment

Check out webmock. That's really what you want to use for something like this.

share|improve this answer
add comment

You're stubbing new with 0 parameters, when you do this:


But your test is:

Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN)

which is new with 2 params.



and remove:

share|improve this answer
add comment

Your Answer


