黄瓜测试重定向

发布于 2024-10-16 00:43:23 字数 799 浏览 3 评论 0原文

找到了一些建议: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

绾颜 2024-10-23 00:43:23

您是否使用 WebRat 或 Capybara 作为 Cucumber 中的驱动程序?您可以通过查看 features/support/env.rb 来判断。我使用的是 Capybara,所以我的包含以下几行:

  require 'capybara/rails'
  require 'capybara/cucumber'
  require 'capybara/session'

默认值曾经是 WebRat,但最近切换到 Capybara,因此网络上旧示例中的许多代码无法正常工作。假设您也使用 Capybara...

request.request_uri - 您需要 current_url 。它返回驱动程序所在页面的完整 URL。这比只获取路径没那么有用,所以我使用这个帮助器:

def current_path
  URI.parse(current_url).path
end

response.should be_success - 使用 Capybara(以及在某种程度上,Cucumber)工作的最大挫折之一是它是完全热衷于只与用户可以看到的内容进行交互。您无法使用水豚测试响应代码。相反,您应该测试用户可见的响应。重定向很容易测试;只需断言您应该位于哪个页面即可。 403 有点诡计。在我的应用程序中,该页面的标题为“访问被拒绝”,因此我只是对此进行测试:

within('head title') { page.should_not have_content('Access Denied') }

以下是我编写一个场景来测试有时应该重定向而其他时候不应该重定向的链接的方法:

Scenario: It redirects
  Given it should redirect
  When  I click "sometimes redirecting link"
  Then  I should be on the "redirected_to" page

Scenario: It does not redirect
  Given it shouldn't redirect
  When  I click "sometimes redirecting link"
  Then  <assert about what should have happened>

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:

  require 'capybara/rails'
  require 'capybara/cucumber'
  require 'capybara/session'

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:

def current_path
  URI.parse(current_url).path
end

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:

within('head title') { page.should_not have_content('Access Denied') }

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:

Scenario: It redirects
  Given it should redirect
  When  I click "sometimes redirecting link"
  Then  I should be on the "redirected_to" page

Scenario: It does not redirect
  Given it shouldn't redirect
  When  I click "sometimes redirecting link"
  Then  <assert about what should have happened>
圈圈圆圆圈圈 2024-10-23 00:43:23

您可以像这样测试响应代码:

Then /^I should get a response with status (\d+)$/ do |status|
  page.driver.status_code.should == status.to_i
end

You can test the response code like this:

Then /^I should get a response with status (\d+)$/ do |status|
  page.driver.status_code.should == status.to_i
end
っ左 2024-10-23 00:43:23

有时用黄瓜做这种事确实有意义,
但最好避免。

不能使用控制器规格吗?

It does sometime make sense to use cucumber for this kinda thing,
but its best avoided.

Can you not use a controller spec?

骷髅 2024-10-23 00:43:23

我发现这是一个老问题,但我想添加我的答案,可能有人觉得它有帮助。
Cucumber 是一个端到端的验收(或者很多人所说的集成)测试框架,这意味着在使用它时,你不应该关心中间状态,而是关心请求的开始(即从哪里发起请求) :单击按钮或链接、从地址栏导航等)以及最终结果(即加载完成后用户将在页面上看到的内容)。

这里你需要的是一个控制器规范,最好由 Rspec 指定,这将使你更好地访问操作级别的中间状态。
一个例子可以是:

describe AController do
  #specify the action where you expect a redirect
  describe 'GET action' do # or POST action
    get :action # or post with post params if the request expected to be a form submition
    expect(response).to be_redirect 
  end
end

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:

describe AController do
  #specify the action where you expect a redirect
  describe 'GET action' do # or POST action
    get :action # or post with post params if the request expected to be a form submition
    expect(response).to be_redirect 
  end
end
下雨或天晴 2024-10-23 00:43:23

一般来说,不建议在 cucumber 中测试当前的 url、响应代码和标头。我建议您为此目的编写更多低级测试: 功能控制器测试请求规范

回答你的问题:

Then /^I should be on the (.+?) page$/ do |page_name|
  expect(current_url).to eq send("#{page_name.downcase.gsub(' ','_')}_path")
end

Then /^I should be redirected to the (.+?) page$/ do |page_name|
  expect(current_url).to eq send("#{page_name.downcase.gsub(' ','_')}_path")
end

水豚自动执行重定向

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:

Then /^I should be on the (.+?) page$/ do |page_name|
  expect(current_url).to eq send("#{page_name.downcase.gsub(' ','_')}_path")
end

Then /^I should be redirected to the (.+?) page$/ do |page_name|
  expect(current_url).to eq send("#{page_name.downcase.gsub(' ','_')}_path")
end

Capybara performs redirects automatically

对你而言 2024-10-23 00:43:23

我只需要测试同样的事情并为 Cabybara 1.1.2 提出以下内容

Then /^I should be on the (.*) page$/ do |page_name|
  object = instance_variable_get("@#{page_name}")
  page.current_path.should == send("#{page_name.downcase.gsub(' ','_')}_path", object)
  page.status_code.should == 200
end

Then /^I should be redirected to the (.*) page$/ do |page_name|
  page.driver.request.env['HTTP_REFERER'].should_not be_nil
  page.driver.request.env['HTTP_REFERER'].should_not == page.current_url
  step %Q(I should be on the #{page_name} page)
end

I just had to test the same thing and came up with the following for Cabybara 1.1.2

Then /^I should be on the (.*) page$/ do |page_name|
  object = instance_variable_get("@#{page_name}")
  page.current_path.should == send("#{page_name.downcase.gsub(' ','_')}_path", object)
  page.status_code.should == 200
end

Then /^I should be redirected to the (.*) page$/ do |page_name|
  page.driver.request.env['HTTP_REFERER'].should_not be_nil
  page.driver.request.env['HTTP_REFERER'].should_not == page.current_url
  step %Q(I should be on the #{page_name} page)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文