【问题标题】:How do I compare xml output in a Cucumber step using a multiline string example?如何使用多行字符串示例比较 Cucumber 步骤中的 xml 输出?
【发布时间】:2011-07-01 14:47:13
【问题描述】:

Chargify 在他们的docs 中有这个 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:
  """
  <?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,而不是像在遇到此示例之前那样逐个检查标签。

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

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

更新

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

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

【问题讨论】:

  • 对于在非 Rails 项目上尝试此功能的任何人,请注意,您将需要 'active_support/core_ext/hash/conversions' 和 'active_support/core_ext/hash/diff'
  • 还要注意,如果你有一个深度嵌套的哈希,这种方法似乎并没有完全遍历它来检查所有的差异。请注意确定原因。

标签: ruby-on-rails ruby xml api cucumber


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您可以使用Hash.from_xml,然后与Hash.diff 进行比较。比较为散列消除了无意义的空白,以免混淆比较。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多