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

发布于 2024-10-18 11:43:07 字数 1797 浏览 2 评论 0原文

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      | [email protected] |
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>[email protected]</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

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      | [email protected] |
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>[email protected]</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'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.

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?

Update

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

oО清风挽发oО 2024-10-25 11:43:07

您可以使用 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.

我喜欢麦丽素 2024-10-25 11:43:07

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

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

In case you don't want the dependency on ActiveSuport (for instance, if you are outside Rails), you might try the equivalent-xml gem. It allows you to write the above expectation as follows:

response = page.body
response.should be_equivalent_to(xml_output)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文