How to Test for a Redirect with Rspec and Capybara

Test Redirection with RSpec and Capybara (Rails)

Try current_path.should == "/projects/show"

Capybara also implements a current_url method for the fully qualified URL.

More info in the docs.

EDIT

Capybara now has a RSpec matcher called have_current_path, which you can use like: expect(page).to have_current_path(some_other_page_path) (thanks @bjliu)

How to test for a redirect with Rspec and Capybara

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')

test page redirection with Capybara + RSpec

Don't use the eq matcher with current_path or current_url. Instead, use the have_current_path matcher provided by Capybara 2.5+

expect(page).to have_current_path(Urls[target], url: true)

The have_current_path matcher uses Capybara's waiting/retrying behavior, so it will wait for the page to change. I added the url: true option to make it compare the full url. If Urls[target] resolves to just a path, you can remove the option.

RSpec, Capybara: redirect_to not working in create/post specs

You have a number of issues in your test.

First, Capybara is not meant to be used in request specs - https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec - but should instead be used with feature/system tests. Once you've fixed that you should no longer need to include Capybara into every RSpec test and should remove the config.include Capybara::DSL from you config (when you require capybara/rspec it includes Capybara into the test types it should be included in - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/rspec.rb#L10)

Second, click_button is not guaranteed to wait for any actions it triggers to complete. Because of that you need to wait for a visual page change before attempting to access any database objects that would be created by that action (technically you really shouldn't be doing direct DB access in feature specs at all but if you're going to ...)

  click_button 'Create' 
expect(page).to have_text('Stamp created!') # Whatever message is shown after creation
# Now you can safely access the DB for the created stamp

Third, as pointed out by @chumakoff you should not be using static matchers with Capybara and should instead be using the matchers provided by Capybara

  click_button 'Create' 
expect(page).to have_text('Stamp created!') # Whatever message is shown after creation
expect(page).to have_path(stamp_path(Stamp.first.id))

Finally, you should look at your test.log and use save_and_open_screenshot to see what your controllers actually did - It's and there's actually an error being raised on creation which is causing your app to redirect to /stamps and display the error message (would also imply your test DB isn't actually being reset between tests, or the factories you show are creating nested records, etc).

Update: After rereading your controller code I noticed the that you're passing a 201 status code to redirect_to. 201 won't actually do a redirect - From the redirect_to docs - https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to

Note that the status code must be a 3xx HTTP code, or redirection will
not occur.

How to follow redirect to subdomain in capybara RSpec test?

This did it for me with Rails 5 and Capybara Chrome driver:

Rails.application.routes.default_url_options[:host] = 'lvh.me'

RSpec + Capybara: redirect_to external page sends me back to root_path

Since you don't specify what driver you are using with Capybara - https://github.com/teamcapybara/capybara#drivers - I assume you're using the default rack_test driver.

The rack_test driver doesn't support requests to external urls (the domain info is just ignored and all paths are routed directly to the AUT) so your test isn't actually testing what you think it is and redirect_to 'https://some.other.website' is actually just redirecting to / in your local app (because the rack_test driver sees 'https://some.other.website/' , ignores all the domain stuff and just treats it as '/' in your app under test).

If you happen to be using one of the other drivers Capybara supports that does support external URLs (selenium, poltergeist, capybara-webkit, etc.) then your WebMock isn't doing what you think it is, because it only controls requests your AUT makes, it doesn't control anything the "browsers" used by those drivers do so they would be free to make requests to outside URLs.

The functionality you are attepting to test is much more suited to be tested through a request spec - https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec - than through a feature/system spec.

Testing a login redirect with Rspec and Capybara

Have you tried manually going to your home page, logging in with those credentials, and then seeing what happens? If you're able to do it, then you're able to rule out the idea of it not working.

I would suggest using the built in assertions instead of matching on the name of the URL. Like so:

assert_response(:redirect)
assert_redirected_to(dashboard_path)

You won't have to wait for your button to click, in either case.



Related Topics



Leave a reply



Submit