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:
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!!
Functional testing is critical to the performance of any complex system, as it checks the system against its design documents and specifications to ensure that it performs all of its functions as it should. And, essential to all functional test systems is the ability to collect and process data.
In a normal situation, when creating a new Rails app, you would run the rails new application_name command after installing Rails. Once the skeleton of the new Rails application has been built, you will see a folder called test in the root of the new application, and many subfolders and files within the test directory.
Don't write view tests. You should be able to change copy or HTML classes without breaking your tests. Just assess critical view elements as part of your in-browser integration tests.
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') %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With