【问题标题】:Cucumber test redirect黄瓜测试重定向
【发布时间】:2011-02-03 14:56:47
【问题描述】:

找到了一些建议: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,为什么?

【问题讨论】:

  • 看看调用这些的功能会有很大帮助。那时你通常会有一个request 对象,但前提是你确实提出了请求。
  • 仅供参考 - 在 Capybara 1.1.2 上我必须使用 page.driver.request.env["HTTP_REFERER"]

标签: ruby-on-rails testing cucumber


【解决方案1】:

您在 Cucumber 中使用 WebRat 或 Capybara 作为驱动程序吗?您可以通过查看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)合作的最大挫折之一是它非常热衷于只与用户可以看到的内容进行交互。你can't test for response codes 使用 Capybara。相反,您应该测试用户可见的响应。重定向很容易测试;只需断言您应该在哪个页面上。 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>

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      有时候用黄瓜来做这种事情是有道理的, 但最好避免。

      你不能使用控制器规范吗?

      【讨论】:

        【解决方案4】:

        我看到这是一个老问题,但我想添加我的答案,可能有人觉得它有帮助。 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
        

        【讨论】:

          【解决方案5】:

          一般不建议在cucumber中测试当前url、响应码和headers。我建议您为此编写更多低级测试:functional controller testrequest specs

          回答你的问题:

          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 自动执行重定向

          【讨论】:

            【解决方案6】:

            我只需要测试同样的东西并为 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
            

            【讨论】:

            • 这是错误的,不起作用。你不能这样做:page.driver.request.env['HTTP_REFERER'].should_not == page.current_url。我的意思是,如果您在路径上说:/users/new 并且您提交了一个标准的 rails 样式的休息表单,并且您想检查您是否被重定向到 index,这是很正常的,那么它不会起作用,因为您发送/users 的表单因此引用路径为/users,索引路径为/users。 ddurdiks 的回答中有智慧之语。不要轻易解雇他们。在大多数情况下,您为什么还要关心如何在集成测试中找到路径?
            猜你喜欢
            • 2016-10-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-25
            • 2021-03-20
            相关资源
            最近更新 更多