yhara.jp

Recent Posts

Using login_user with Sorcery gem on Rails 5.2

2018-08-29
Tech

Problem

Sorcery's Testing-Rails guide is based on Rails4 and does not work with Rails5.2.

With include Sorcery::TestHelpers::Rails::Integration, I got:

TasksControllerTest#test_should_get_index:
NameError: undefined local variable or method `page' for #<TasksControllerTest:0x00007fff19df4a10>
    test/controllers/tasks_controller_test.rb:7:in `block in <class:TasksControllerTest>'

With include Sorcery::TestHelpers::Rails::Controller:

TasksControllerTest#test_should_get_index:
NoMethodError: undefined method `auto_login' for nil:NilClass
Did you mean?  autoload
    test/controllers/tasks_controller_test.rb:7:in `block in <class:TasksControllerTest>'

Solution

As mentioned here, add this to test/test_helper.rb (you don't need to include Sorcery::TestHelpers::XX).

# test/test_helper.rb
class ActiveSupport::TestCase
  # ...

  def login_user(user, password='passw0rd')
    user = users(:one)
    post user_sessions_url, params: {email: user.email, password: password}
    follow_redirect!
  end
end

The password is set to the user fixture like this.

# test/fixtures/users.yml
one:
  email: a@b.cd
  salt: <%= salt = "abcd1234abcd1234" %>
  crypted_password: <%= Sorcery::CryptoProviders::BCrypt.encrypt("passw0rd", salt) %>

More posts

Posts

(more...)

Articles

(more...)

Category

Ads

About

About the author