如何比较XML输出​​在使用多行字符串例如黄瓜一步? [英] How do I compare xml output in a Cucumber step using a multiline string example?

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

问题描述

Chargify在其文档这种黄瓜方案。

 方案:通过检索我的参考ID的客户(如一个整数或简单的字符串)
由于我有这些属性的客户
  |参考| FIRST_NAME |姓氏|电子邮件|
  | 7890 |乔|吹| joe@example.com |
当我发送一个GET请求到https:// [@subdomain] .chargify.com /客户/ lookup.xml参考= 7890
然后响应状态应为200 OK
和响应应该是XML:
  
  < XML版本=1.0编码=UTF-8&GT?;
  <客户>
    < ID类型=整数>`自动generated`< / ID>
    <&FIRST_NAME GT;乔< / FIRST_NAME>
    <&姓氏GT;&吹LT; /姓氏>
    &LT;电子邮件和GT; joe@example.com< /电子邮件&GT;
    &LT;组织&GT;`你value`&LT; /组织&GT;
    &lt;参考&GT; 7890&LT; /参考&GT;
    &LT; created_at类型=日期时间&GT;`自动generated`&LT; / created_at&GT;
    &LT;的updated_at类型=日期时间&GT;`自动generated`&LT; /&的updated_at GT;
  &LT; /客户&GT;
  

我试图按照测试我们在这里开发一个API他们的做法,而不是逐个检查标签,就像我我们碰到这个例子来之前做的事情。

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

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

更新

下面是一个使用马克的解决方案黄瓜步:

 然后/ ^我应该看到下面的输出$ /做| xml_output |
  响应= Hash.from_xml(page.body)
  预计= Hash.from_xml(xml_output)
  expected.diff(响应)。应== {}
结束


解决方案

您可以使用 Hash.from_xml 然后用 Hash.diff 比较。作为哈希比较来自搞乱比较消除不重要的空白。

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>
  """

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

解决方案

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

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

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