简体   繁体   中英

How can I use Cucumber to test Devise's Rememberable functionality?

I'd like to have a Cucumber feature testing the rememberable functionality of devise (a remember me cookie).

It is easy to check the remember me check box using capybara but how should I simulate a user returning to the site after closing their window?

I came up with the following rack-test hack, and slightly cleaner selenium api use, to test Devise remember-me functionality in cucumber/capybara. It just tells the driver to manually erase the session cookie. Not all drivers are supported, I only implemented the two I've used:

http://gist.github.com/484787

This assumes cookie storage of the session. Remove @announce tag from the scenario to get rid of the verbosity.

Another option, suggested by Matt Wynne in the mailing list discussion , may be looking at other cookie stores, and deleting them by query or file deletion:

lifted from agile rails book:

config.action_controller.session_store = CGI::Session::PStore (or just :p_store)
config.action_controller.session_options[:tmpdir] = "/Users/dave/tmp" 
config.action_controller.session_options[:prefix] = "myapp_session_"

or

rake db:sessions:create
config.action_controller.session_store = :active_record_store

Rails also has a reset session method, but I believe we don't have access to this because we can't hook into the rails session when testing with capybara.

Hope this helps,

Nick

nruth's gist was really helpful but I felt like deleting the cookie by name was cheating. I created a step that deletes the cookies a browser would delete when it was closed and restarted (any cookie without an expiry date set and set in the future).

You can see it in this commit (though I've only done it for the RackTest driver as I don't have Selenium setup). You can also see my login/remember_me feature in this commit . And I refactored the classes to separate files in this commit .

I hope that's helpful.

You can use show_me_the_cookies for this as shown below:

And(/^I leave the site$/) do
  expire_cookies
end

I used email-spec to accomplish this. My Scenario looks like the following:

@allow-rescue
Scenario: Create New Account (Everything cool)
Given I am not authenticated
When I go to register
And I fill in "Name" with "bill"
And I fill in "Email" with "bill@example.com"
And I fill in "Password" with "please"
And I fill in "Password Confirmation" with "please"
And I press "Sign up"
Then "bill@example.com" should receive an email
And I open the email
And I should see "Confirm my account" in the email body
When I follow "Confirm my account" in the email
Then I should see "Your account was successfully confirmed. You are now signed in."

Note the @allow-rescue decoration above the scenario that is necessary when using Devise.

Hope this helps.

I guess you could log out with capybara, and then log back in something like

Given I am on the login screen
And I select 'Remember Me'
And I click 'login'
Then I should be 'logged in'
When I click 'log out'
Then I should be 'logged out' #=> potentially destroy a session here?
When I click log in
Then I should be logged in
And I should not be directed to the login form.

This should use cabybara's current cookie state to model this flow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM