Mocking a Faraday::Response object's `body`, and Nokogiri::XML::NodeSet

2.7k views Asked by At

When trying to mock a Faraday::Response object, I've tried:

mock(Faraday::Response, :env => {:body => "...some xml..."})

...but this fails when I call response.body.xpath(...), because the body is a String, and it's expecting a NodeSet.

Is there an easy way to convert an arbitrary XML string into a NodeSet? I've read through the Nokogiri::XML::NodeSet docs, but it's either not there, or I'm missing it.

Here's my actual stub+mock:

@conn.stub!(:get).and_return(mock Faraday::Response,
      :env => {:status=>207},
      :body => '<scheduled-calendar xmlns="[removed]" xmlns:xsi="[removed]" id="95ec6d99-2c4a-4e25-99a1-2e988c0dbce6" assignment-id="uuid_for_test" xsi:schemaLocation="[removed]" debug="true">
                <scheduled-study-segment id="4e5b24e3-b50a-45e2-aec1-cb6c839a20f1" start-date="2012-01-06" start-day="1" study-segment-id="524910e9-634a-4791-a0b2-90f506a86474">
                <scheduled-activity id="1caa08b2-acb0-4c93-8c76-6460f0e9ee09" ideal-date="2012-01-06" details="Screening survey" repetition-number="0" planned-activity-id="1fc520dd-5b6f-4125-b83c-2f8801ea3065">
                <current-scheduled-activity-state reason="Initialized from template" date="2012-01-06" state="conditional"/>
                </scheduled-activity>
                </scheduled-study-segment>
                </scheduled-calendar>'
    )

Here's the call to xpath

response.body.xpath('//psc:scheduled-activity', Psc.xml_namespace).collect{|activity| activity.attributes["id"].value}

And here is the test failure:

NoMethodError in 'PscV1 should allow you to get a list of scheduled activity ids for a given involvement'
undefined method `xpath' for #<String:0x10dad1818>
1

There are 1 answers

0
jefflunt On BEST ANSWER

So, essentially the answer turned out to be that Faraday wasn't treating my body as XML, and as a result, just passed the response back as a String, instead of a Nokogiri::XML::Document

By doing...

xml = Nokogiri::XML('<scheduled-calendar xmlns="[removed]" xmlns:xsi="[removed]" id="95ec6d99-2c4a-4e25-99a1-2e988c0dbce6" assignment-id="uuid_for_test" xsi:schemaLocation="[removed]" debug="true">
            <scheduled-study-segment id="4e5b24e3-b50a-45e2-aec1-cb6c839a20f1" start-date="2012-01-06" start-day="1" study-segment-id="524910e9-634a-4791-a0b2-90f506a86474">
            <scheduled-activity id="1caa08b2-acb0-4c93-8c76-6460f0e9ee09" ideal-date="2012-01-06" details="Screening survey" repetition-number="0" planned-activity-id="1fc520dd-5b6f-4125-b83c-2f8801ea3065">
            <current-scheduled-activity-state reason="Initialized from template" date="2012-01-06" state="conditional"/>
            </scheduled-activity>
            </scheduled-study-segment>
            </scheduled-calendar>')

And then passing that xml variable in as the body...

@conn.stub!(:get).and_return(mock Faraday::Response,
  :env => {:status=>207},
  :body => xml
)

...the issue was resolved.