如何使用多行字符串示例比较 Cucumber 步骤中的 xml 输出? [英] How do I compare xml output in a Cucumber step using a multiline string example?

查看:30
本文介绍了如何使用多行字符串示例比较 Cucumber 步骤中的 xml 输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Chargify 在他们的 docs 中有这个 Cucumber 场景.

Chargify has this Cucumber scenario in their 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>
  """

我正在尝试按照他们的方法来测试我们在此处开发的 API,而不是像我在遇到此示例之前那样逐个检查标签.

I'm trying to follow their approach in testing an API we're developing here, instead of checking for the tags one by one, like I we were doing before coming across this example.

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

So far no luck matching the response's output with the step's multiline string, due to formatting issues. Haven't found a way with Nokogiri nor with simple stripped string comparison.

有没有一种优雅(且有效)的方法来做到这一点?

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

更新

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

Here's the cucumber step using Mark's 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

推荐答案

您可以使用 Hash.from_xml 然后与 Hash.diff 进行比较.以散列形式进行比较可消除无关紧要的空白,以免混淆比较.

You can use Hash.from_xml and then compare with Hash.diff. Comparing as hashes eliminates insignificant whitespace from messing up comparisons.

这篇关于如何使用多行字符串示例比较 Cucumber 步骤中的 xml 输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆