Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails integration test with the devise gem

I want to write an rails integration test (with ActionDispatch::IntegrationTest). I am using devise for authentication and machinist for test models. I cannot successfully sign in.

Here is a simple example:

class UserFlowsTest < ActionDispatch::IntegrationTest
  setup do
    User.make
  end
  test "sign in to the site" 
    # sign in
    post_via_redirect 'users/sign_in', :email => '[email protected]', :password => 'qwerty'    
    p flash
    p User.all
end

Here is the debug output:

Loaded suite test/integration/user_flows_test
Started
{:alert=>"Invalid email or password."}
[#<User id: 980190962, email: "", encrypted_password: "", password_salt: "", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2010-11-27 16:44:10", updated_at: "2010-11-27 16:44:10">, #<User id: 980190963, email: "[email protected]", encrypted_password: "$2a$10$vYSpjIfAd.Irl6eFvhJL0uAwp4qniv5gVl4O.Hnw/BUR...", password_salt: "$2a$10$vYSpjIfAd.Irl6eFvhJL0u", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2010-11-27 17:09:13", updated_at: "2010-11-27 17:09:13">]
"/unauthenticated"
F

Here is my blueprints.rb:

require 'machinist/active_record'
require 'sham'

User.blueprint do
  email {"[email protected]"}
  password {"qwerty"}
end
like image 498
hiwaylon Avatar asked Nov 27 '10 17:11

hiwaylon


People also ask

Does devise work with rails 7?

Our out-of-the box Devise setup is now working with Rails 7. Once again, if you'd like to refer to any of the code for this setup, or use the template wholesale for a new app, the code is available on GitHub, and you may also use it as a template repo to kick off your own Rails 7 devise projects.

What is devise Gem in Ruby on Rails?

Devise is the cornerstone gem for Ruby on Rails authentication. With Devise, creating a User that can log in and out of your application is so simple because Devise takes care of all the controllers necessary for user creation ( users_controller ) and for user sessions ( users_sessions_controller ).


1 Answers

The reason it doesn't work is devise creates form field names as

'user[email]'
'user[password]'

and post_via_redirect expects those names as arguments. So following statement would make a successful login.

post_via_redirect 'users/sign_in', 'user[email]' => '[email protected]', 'user[password]' => 'qwerty' 
like image 97
burhanyasar Avatar answered Oct 19 '22 18:10

burhanyasar