使用 Mongoid 进行 Cucumber 和 DELETE
因此,我目前正在尝试测试与 Mongoid 和 Cucumber 连接的 Rails 应用程序。我已经完成了所有设置(或者我相信),以便运行以下测试:
Feature: Create and manage about entries
In order to use the about data effectively
As an application consumer
I want to create and manage some about entries
Scenario: Create about entries
Given the following abouts exist
| title | body_copy |
| "About entry #1" | "hello body!" |
When I go to the list of about entries
Then I should see "About entry #1"
Scenario: Create and retreive specific about entry
Given the following abouts exist
| id | title | body_copy |
| 4e4d37756ea257f031000003 | "About entry #1" | "hello body!" |
When I go to about entry with id 4e4d37756ea257f031000003
Then I should see "About entry #1"
在我的路径文件中,我有以下条目来支持上述测试:
when /^the list of about entries$/i
'/abouts'
when /^about entry with id (.+)$/i
"/abouts/#{$1}"
这些测试效果很好。但是,我需要测试删除操作。我在网上做了一些研究,但一切似乎都是通过 UI 来删除这些项目,我遇到的问题是我的 Rails 应用程序只提供 JSON 文件和 JSON 文件,我需要一种更好的(更具编程性的)方法来测试事物,而无需UI 参与。就模拟而言,我使用的是 Pickle 中内置的默认模拟。如有必要,我愿意使用其他模拟软件,例如 Factory-girl,但您必须给我一些详细的反馈,我如何连接它。我目前的删除测试(不起作用)是:
Scenario: Delete about
Given the following abouts exist
| title | body_copy |
| title 1 | body_copy 1 |
| title 2 | body_copy 2 |
| title 3 | body_copy 3 |
| title 4 | body_copy 4 |
When I delete the 3rd about
Then I should see the following abouts:
| Title | body_copy |
| title 1 | body_copy 1 |
| title 2 | body_copy 2 |
| title 4 | body_copy 4 |
问题是自动生成的测试(如上所示)使用 click_link "Destroy"
方法调用,但这不起作用。
So, I am currently trying to test a Rails app wired up with Mongoid with Cucumber. I have everything setup (or so I believe) so that the following test will run:
Feature: Create and manage about entries
In order to use the about data effectively
As an application consumer
I want to create and manage some about entries
Scenario: Create about entries
Given the following abouts exist
| title | body_copy |
| "About entry #1" | "hello body!" |
When I go to the list of about entries
Then I should see "About entry #1"
Scenario: Create and retreive specific about entry
Given the following abouts exist
| id | title | body_copy |
| 4e4d37756ea257f031000003 | "About entry #1" | "hello body!" |
When I go to about entry with id 4e4d37756ea257f031000003
Then I should see "About entry #1"
In my paths file, I have the following entries to support the above tests:
when /^the list of about entries$/i
'/abouts'
when /^about entry with id (.+)$/i
"/abouts/#{$1}"
These tests work great. However, I need to test the delete action. I did some research online but everything seems to be going through the UI to delete these items and the problem I have is that my Rails app only serves JSON files and JSON files and I need a better (more programatic) way of testing things without the UI being involved. As far as mocks goes, I am using the default mocks built into Pickle. I am open to using other mocking software if necessary such as factory-girl, but you'll have to give me some detailed feedback how I can wire that up. What I have currently for my delete test (that DOESN'T work) is:
Scenario: Delete about
Given the following abouts exist
| title | body_copy |
| title 1 | body_copy 1 |
| title 2 | body_copy 2 |
| title 3 | body_copy 3 |
| title 4 | body_copy 4 |
When I delete the 3rd about
Then I should see the following abouts:
| Title | body_copy |
| title 1 | body_copy 1 |
| title 2 | body_copy 2 |
| title 4 | body_copy 4 |
The problem is that the auto-generated test (seen above) uses the click_link "Destroy"
method call but that will not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要更改删除步骤的实现(如果您使用 Cucumber 生成的默认值,则在您的 web_steps.rb 中)以发送 DELETE HTTP 请求。我建议使用 RestClient gem,但还有很多其他选择。
PragProg 目前正在测试的 Cucumber Book 有一章是关于使用 Cucumber 测试 REST API 的,如下所示。
You'll need to change the implementation of the delete step (in your web_steps.rb if your using the Cucumber generated defaults) to send a DELETE HTTP request. I'd recommend the RestClient gem for this, but there are plenty other choices.
The Cucumber Book currently in beta from PragProg has a chapter about using Cucumber to test REST APIs like this.