2

I call out to an API, where I then take their xml and parse it to return a Response object.

Typical stuff for calling a 3rd party API.

So now in my tests I want to use webmock to say "When you get a request to go to example.com, just give me a Response.new object so i can continue on with my code and process it`.

I am using rspec. Here is the code thus far.

Test code is:

stub_request(:any, /.*blah.*/).
  with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
  to_return(:status => 200, 
            :body => "#{BlahResponse.new("<?xml version=\"1.0\"?><data><type><![CDATA[success]]></type><subject><![CDATA[Info]]></subject><code>29</code><description><![CDATA[Success]]></description><meeting_id><![CDATA[awesome-455]]></meeting_id></data>")}",
            :headers => {})

The error always results in response is not a method of Nil, meaning I am not passing anything it looks like.

Chris Salzberg
  • 27,099
  • 4
  • 75
  • 82
pjammer
  • 9,489
  • 5
  • 46
  • 56
  • Won't you be creating the Response object in the API code when you make the request? It doesn't make sense to stub a request to a URL to return an object, because that can't actually happen. In your code you would be taking the status, body and headers and assigning them to a new Request object, no? – Chris Salzberg Aug 31 '12 at 21:35
  • Yes. I tried stuff that XML in there, but it's not parsing like my. Normal code does. Shall I rephrase question to say how do I return XML properly? – pjammer Aug 31 '12 at 21:55
  • Hmm... then you should post the code where you're parsing the XML. The stub will just pass on whatever you put in there. – Chris Salzberg Aug 31 '12 at 22:04
  • everything in the quotes between .new(" and ") i put in the response in quotes... ugh. – pjammer Sep 01 '12 at 16:57
  • I'm not sure I understand. Is this still a problem? If so it would help if you could post the code where you make the request. This shouldn't be hard to fix. – Chris Salzberg Sep 01 '12 at 22:38
  • 2
    the problem was PEBKAC in a way for this question. in the `to_return` instead of just `:status => 200` i should have put `:status => [200, "OK"]` – pjammer Sep 03 '12 at 02:11

0 Answers0