title: rspec mocking external api
body: <p>New to TDD here, d'oh!</p>
  
  <p>Here's what I want to test (<a href="https://www.twilio.com/docs/ruby/install" rel="nofollow">ruby library</a>), in brief:</p>
  
  <pre><code> account = Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN)
      resp = account.request(
          "/#{API_VERSION}/Accounts/#{ACCOUNT_SID}/SMS/Messages",
          'POST',
          smsInfo
      )
  </code></pre>
  
  <p>And here's test code attempt:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>which generates the error:  undefined method `request' for nil:NilClass</p>
  
  <p>Am I taking the right approach? thanks!</p>