【发布时间】: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