Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for a redirect with Rspec and Capybara

I don't know what I'm doing wrong, but every time I try to test for a redirect, I get this error: "@request must be an ActionDispatch::Request"

context "as non-signed in user" do
  it "should redirect to the login page" do
    expect { visit admin_account_url(account, host: get_host(account)) }.to redirect_to(signin_path)
  end
end
1) AdminAccountPages Admin::Accounts#show as non-signed in user should redirect to the login page
     Failure/Error: expect { visit admin_account_url(account, host: get_host(account)) }.to redirect_to(signin_path)
     ArgumentError:
       @request must be an ActionDispatch::Request
     # ./spec/requests/admin_account_pages_spec.rb:16:in `block (4 levels) in <top (required)>'

I'm using RSpec-rails (2.9.0) with Capybara (1.1.2) and Rails 3.2. I would appreciate it if someone could also explain why this is happening; why can't I use the expect in such a way?

like image 466
Mohamad Avatar asked Jun 08 '12 20:06

Mohamad


2 Answers

Capybara is not a rails-specific solution so it doesn't know anything about rails's rendering logic.

Capybara is meant specifically for Integration testing, which is essentially running tests from the viewpoint of an end-user interacting with a browser. In these tests, you should not be asserting templates because an end-user can't see that deep into your application. What you should instead be testing is that an action lands you on the correct path.

current_path.should == new_user_path
page.should have_selector('div#erro_div')
like image 131
Sagiv Ofek Avatar answered Nov 11 '22 20:11

Sagiv Ofek


you can do it this way:

expect(current_path).to eql(new_app_user_registration_path)
like image 21
The Whiz of Oz Avatar answered Nov 11 '22 21:11

The Whiz of Oz