黄瓜测试重定向
找到了一些建议: http:// openmonkey.com/articles/2009/03/cucumber-steps-for-testing-page-urls-and-redirects
我已将上述方法添加到我的 Web 步骤定义中,编写了我的功能,运行它并得到关于 nil 对象的错误。经过一番调查,我注意到,我没有响应和请求对象,它们都是 nil
来自 web_steps.rb:
Then /^I should be on the (.+?) page$/ do |page_name|
request.request_uri.should == send("#{page_name.downcase.gsub(' ','_')}_path")
response.should be_success
end
Then /^I should be redirected to the (.+?) page$/ do |page_name|
request.headers['HTTP_REFERER'].should_not be_nil
request.headers['HTTP_REFERER'].should_not == request.request_uri
Then "I should be on the #{page_name} page"
end
请求和响应对象都是 nil,为什么?
Have found some advice: http://openmonkey.com/articles/2009/03/cucumber-steps-for-testing-page-urls-and-redirects
I have added the above methods to my web steps definitons, have written my feature, ran it and got an error about nil objects. After some investigation, I have noticed, I have no response and request objects, they are nil
From web_steps.rb:
Then /^I should be on the (.+?) page$/ do |page_name|
request.request_uri.should == send("#{page_name.downcase.gsub(' ','_')}_path")
response.should be_success
end
Then /^I should be redirected to the (.+?) page$/ do |page_name|
request.headers['HTTP_REFERER'].should_not be_nil
request.headers['HTTP_REFERER'].should_not == request.request_uri
Then "I should be on the #{page_name} page"
end
The request and response objects are nil, why ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您是否使用 WebRat 或 Capybara 作为 Cucumber 中的驱动程序?您可以通过查看
features/support/env.rb
来判断。我使用的是 Capybara,所以我的包含以下几行:默认值曾经是 WebRat,但最近切换到 Capybara,因此网络上旧示例中的许多代码无法正常工作。假设您也使用 Capybara...
request.request_uri - 您需要 current_url 。它返回驱动程序所在页面的完整 URL。这比只获取路径没那么有用,所以我使用这个帮助器:
response.should be_success - 使用 Capybara(以及在某种程度上,Cucumber)工作的最大挫折之一是它是完全热衷于只与用户可以看到的内容进行交互。您无法使用水豚测试响应代码。相反,您应该测试用户可见的响应。重定向很容易测试;只需断言您应该位于哪个页面即可。 403 有点诡计。在我的应用程序中,该页面的标题为“访问被拒绝”,因此我只是对此进行测试:
以下是我编写一个场景来测试有时应该重定向而其他时候不应该重定向的链接的方法:
Are you using WebRat or Capybara as your driver within Cucumber? You can tell by looking at
features/support/env.rb
. I'm using Capybara, so mine includes these lines:The default used to be WebRat but switched recently to Capybara, so a lot of the code from older examples on the web doesn't work properly. Assuming you're using Capybara too...
request.request_uri - You want current_url instead. It returns the full URL of the page your driver is on. This is less useful than getting just the path, so I use this helper:
response.should be_success - One of the biggest frustrations with working with Capybara (and to a certain extent, Cucumber) is that it is downright zealous about only interacting with what the user can see. You can't test for response codes using Capybara. Instead, you should test user-visible responses. Redirects are easy to test; just assert what page you should be on. 403s are a little tricker. In my application, that's a page where the title is "Access Denied," so I just test for that:
Here's how I'd write a scenario to test a link that is supposed to redirect sometimes and not supposed to redirect at other times:
您可以像这样测试响应代码:
You can test the response code like this:
有时用黄瓜做这种事确实有意义,
但最好避免。
不能使用控制器规格吗?
It does sometime make sense to use cucumber for this kinda thing,
but its best avoided.
Can you not use a controller spec?
我发现这是一个老问题,但我想添加我的答案,可能有人觉得它有帮助。
Cucumber 是一个端到端的验收(或者很多人所说的集成)测试框架,这意味着在使用它时,你不应该关心中间状态,而是关心请求的开始(即从哪里发起请求) :单击按钮或链接、从地址栏导航等)以及最终结果(即加载完成后用户将在页面上看到的内容)。
这里你需要的是一个控制器规范,最好由 Rspec 指定,这将使你更好地访问操作级别的中间状态。
一个例子可以是:
I see this is an old question, but I want to add my answer, may be someone find it helpful.
Cucumber is an end-to-end acceptance (or integration as many call it) test framework, it means that when using it, you should not care about the intermediate states, but the beginning of a request (i.e. from where the request is initiated: click on a button or link, navigation from the address bar and so on), and the final result (i.e. what the user will see on the page when the load completed).
What you need here is a Controller spec, that is better to be specified by Rspec, which will give you better access to intermediates states in the action level.
An example can be:
一般来说,不建议在 cucumber 中测试当前的 url、响应代码和标头。我建议您为此目的编写更多低级测试: 功能控制器测试或请求规范
回答你的问题:
水豚自动执行重定向
Generally it is not recommended to test current url, response codes and headers in cucumber. I would advice you to write more low level test for this purpose: functional controller test or request specs
To answer your question:
Capybara performs redirects automatically
我只需要测试同样的事情并为 Cabybara 1.1.2 提出以下内容
I just had to test the same thing and came up with the following for Cabybara 1.1.2