Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber test redirect

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 ?

like image 718
astropanic Avatar asked Feb 03 '11 14:02

astropanic


2 Answers

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>
like image 89
ddurdik Avatar answered Nov 17 '22 10:11

ddurdik


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
like image 29
Houen Avatar answered Nov 17 '22 08:11

Houen