如何使用多行字符串示例比较 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 | [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 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.
如果您不希望依赖 ActiveSuport(例如,如果您在 Rails 之外),您可以尝试 等效的 xml gem。它允许您将上述期望写成如下:
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: