How to compare xml output at Cucumber stage using multiline string example?

Charge has this cucumber script in docs .

Scenario: Retrieve a customer via my reference id (as an integer or simple string) Given I have a customer with these attributes | reference | first_name | last_name | email | | 7890 | Joe | Blow | joe@example.com | When I send a GET request to https://[@subdomain].chargify.com/customers/lookup.xml?reference=7890 Then the response status should be "200 OK" And the response should be the xml: """ <?xml version="1.0" encoding="UTF-8"?> <customer> <id type="integer">`auto generated`</id> <first_name>Joe</first_name> <last_name>Blow</last_name> <email> joe@example.com </email> <organization>`your value`</organization> <reference>7890</reference> <created_at type="datetime">`auto generated`</created_at> <updated_at type="datetime">`auto generated`</updated_at> </customer> """ 

I am trying to follow their approach when testing the API that we are developing here, instead of checking the tags one by one, as I did before I met this example.

So far, it has not been possible to combine the response output with a multi-line step line due to formatting problems. They did not find a way with Nokogiri or with a simple devoid of string comparisons.

Is there an elegant (and efficient) way to do this?

Update

Here's the cucumber pitch using the Mark solution:

 Then /^I should see the following output$/ do |xml_output| response = Hash.from_xml(page.body) expected = Hash.from_xml(xml_output) expected.diff(response).should == {} end 
+4
source share
2 answers

You can use Hash.from_xml and then compare with Hash.diff . Comparison with a hash eliminates minor gaps from random comparisons.

+4
source

If you don't need an ActiveSuport dependency (for example, if you're outside of Rails), you can try the equivalent-xml gem . This allows you to write the following expectation as follows:

 response = page.body response.should be_equivalent_to(xml_output) 
+2
source

Source: https://habr.com/ru/post/1340705/


All Articles