如何使用多行字符串示例比较Cucumber步骤中的xml输出?

Chargify在他们的文档中有这个Cucumber场景。

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: """   `auto generated` Joe Blow joe@example.com `your value` 7890 `auto generated` `auto generated`  """ 

我正在尝试按照他们的方法测试我们在这里开发的API,而不是逐个检查标签,就像我在遇到这个例子之前所做的那样。

到目前为止,由于格式问题,没有运气将响应的输出与步骤的多行字符串相匹配。 没有找到Nokogiri的方法,也没有简单的剥离字符串比较。

有一种优雅(和有效)的方式来做到这一点?

更新

这是使用Mark的解决方案的黄瓜步骤:

 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 

您可以使用Hash.from_xml然后与Hash.diff进行比较。 比较哈希值可以消除无关紧要的空白,从而避免混淆比较。

如果您不希望依赖ActiveSuport(例如,如果您在Rails之外),您可以尝试使用equivalent-xml gem 。 它允许您按如下方式编写上述期望:

 response = page.body response.should be_equivalent_to(xml_output)