Testing: executing "visit" command ends session

The code in question is at https://github.com/jhsu802701/generic_app_template/blob/master/test/integration/admins_view_test.rb .

I’ve used the Devise gem to create users and admins. I’ve successfully made the admin index page visible ONLY to admins, but it’s failing the tests (Capybara and minitest). For some reason, executing the “visit” command kills the session. WHY? What should I be doing about this?

The source code is:

login_admin(‘elle_woods@example.com’, ‘Harvard Law’, false)

View admin index

admin_path = root_path + ‘/admins’
click_on ‘Profile’

visit admin_path # NOTE: using the ‘visit’ command loses the session

@a1.reload
#assert_equal current_path, ‘/admins’
assert_text ‘Vivian’
assert_text ‘Kensington’
assert_text ‘Elle’
assert_text ‘Woods’

Hello Jason,

I think need to change some code. You need to test sections of your app
independently, so if you want to check if the view is only available for
Admins, you can login it with test methods provided by Devise and
later, visit the admins_path.

Instead of call login_admin, you can use:
   login_as(user, :scope => :user)
   visit admin_path

   # asserts...

In another test, check if an admin can login with the form (like
login_admin). You need to set Warden to test mode including this line at
top of your integration tests:

   include Warden::Test::Helpers
   Warden.test_mode!

More info at:
https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara

Regards!

The code in question is at https://github.com/jhsu802701/generic_app_template/blob/master/test/integration/admins_view_test.rb .

I've used the Devise gem to create users and admins. I've successfully made the admin index page visible ONLY to admins, but it's failing the tests (Capybara and minitest). For some reason, executing the "visit" command kills the session. WHY? What should I be doing about this?

What do you mean by "kills the session". What happens? How does the test fail?

Fred

The problem is that the session is lost as soon as the “visit” command is invoked. How do I get the test to retain the session?

Also, that link you cited revolves around RSpec instead of Minitest.

Are there any good examples of someone who has successfully done what I’m trying to do? I can’t possibly be the first person to use Minitest for testing the visibility of a page for different types of users.

I get error messages that indicate that nobody is logged in. The admin should be able to view the admin index page, but the users and the general public should not be able to. Unfortunately, the test doesn’t show that the admin can view the admin index page. While this works in my development environment, it doesn’t work in the test environment.

Jason Hsu, Rubyist wrote in post #1175704:

Also, that link you cited revolves around RSpec instead of Minitest.

I'm using Test::Unit instead of RSpec. and the code of warden test mode
works.

  include Warden::Test::Helpers
  Warden.test_mode!