Functional testing with Rails and Devise. What to put in my fixtures?

Jez Caudle picture Jez Caudle · Jan 2, 2011 · Viewed 10k times · Source

Hi I'm wanting to do some functional testing of my Rails 3 app that uses Devise and CanCan.

In my User model I have the users age, I want to test that a user can only visit a certain page if they are:

  1. Logged in
  2. Over 35

I have seen in the Devise document that I can use: *sign_in* and I have put it in my tests and it appears to work - the test doesn't error because I have:

include Devise::TestHelpers

in my *test_helper.rb*

When I take it out my test does error because *sign_in* is not defined. So it is not a helper problem.

When I run the test and check to see if span#loggedin has one occurrence the test fails because there is 0 occurrences. span#loggedin only appears *if user_signed_in?*

What do I need to put in my fixtures or tests to create a test user who is also a fully signed up user (confirmed etc)?

View Code:

<% if user_signed_in? %>
     <span id="loggedin">User is signed in</span>
     User age is <span id="age"><%= current_user.age.to_s %></span>
<% end %>

Test Code:

test "should get index" do
    sign_in :one
    get :index
    assert_response :success
    assert_select 'span#loggedin', :count => 1
end

Fixture:

one:
 email: [email protected]
 age: 36

It works okay in development when I manually login but I'm hoping to automate it all - the point of testing really!!

Answer

ReggieB picture ReggieB · May 3, 2012

Solution for Devise pre 3.2.0

I think this might be what you are looking for:

User.new.send(:password_digest, 'password')

It works when the salt is nil.

And therefore in your fixture you can do this:

one:
  email: '[email protected]'
  encrypted_password: <%= User.new.send(:password_digest, 'password') %>

Solution for Devise versions 3.2.0 to 3.5.0

At Devise 3.2.0, a method was created precisely for this purpose (@Inkling update). For these versions the encrypted_password should be defined like this:

encrypted_password: <%= Devise.bcrypt(User, 'password') %>

where User is the class of your user model.

Note that this only applies if you're using the default encryption algorithm (bcrypt).


Solution for Devise versions 3.5.1 and above

As Katarzyna has pointed out: Device.bcrypt has been deprecated in version 3.5.1. From that version on, the encrypted_password should be defined like this:

encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>