sorcery icon indicating copy to clipboard operation
sorcery copied to clipboard

Test helpers do not work in Rails 5

Open 5t111111 opened this issue 9 years ago • 8 comments

In Rails 5, generated controller tests now inherit from ActionDispatch::IntegrationTest, so the helpers like login_user don't seem to work.

e.g.) the following does not work (think if index requires login):

class PostsControllerTest < ActionDispatch::IntegrationTest
  include Sorcery::TestHelpers::Rails::Integration
  include Sorcery::TestHelpers::Rails::Controller

  setup do
    @user = users(:alice)
  end

  test "should get index" do
    login_user
    get posts_url
    assert_response :success
  end

  ...
end

Are there any idea or plan for using the test helpers in Rails 5 controller testing?

FYI: Devise seems to be going to implement a test helper for integration tests. https://github.com/plataformatec/devise/issues/3913

5t111111 avatar May 08 '16 03:05 5t111111

Hi @5t111111

If your rest is an integration test, you should not include Sorcery::TestHelpers::Rails::Controller. The reason is that both modules that you include contain exactly the same methods, and the 2nd one simply overrides the 1st one.

arnvald avatar May 15 '16 15:05 arnvald

@arnvald I'm sorry for asking you with a wrong snippet. However, even after removing include Sorcery::TestHelpers::Rails::Controller, the problem has not been resolved.

I think the cause of the problem is that page is not available in the context, I've got the following error:

Error:
NameError: undefined local variable or method `page' for #<PostsControllerTest:0x007fd4f72badf8>

I'm going to give it further investigation, but it'll take a while because actually I don't write any controller tests ATM...

Anyway, thank you for the answer out of your busy schedule.

5t111111 avatar May 16 '16 03:05 5t111111

Im having a similar issue.

include Sorcery::TestHelpers::Rails::Integration produces "NameError: undefined local variable or method `sessions_url' for"

include Sorcery::TestHelpers::Rails::Controller produces "NoMethodError: undefined method `auto_login' for nil:NilClass"

I'm still digging into this and will report back if I find solution

atstockland avatar Jul 05 '16 23:07 atstockland

It seems the integration test helpers only work with Rack::Tests. Neither it works for Rails integration tests nor Capybara feature tests.

dnesteryuk avatar Oct 24 '16 16:10 dnesteryuk

Today I got the same problem.

Error:
GroupsControllerTest#test_should_get_index:
NameError: undefined local variable or method `page' for #<GroupsControllerTest:0x007fcd0dc511d8>

calazans10 avatar Nov 04 '16 01:11 calazans10

Workaround:

Change your controller tests to inherit from ActionController::TestCase. Rails 5 tests inherit from ActionDispatch::IntegrationTest.

That being said, you will also need to convert your tests back to the previous syntax. To move forward, not sure what it takes to update the code though.

olimart avatar Nov 11 '16 17:11 olimart

Alternative:

in test_helper.rb, add:

def authenticate_user(user = users(:admin))
  post sessions_url, params: {
    session: { email: user.email, password: "123123" } }
  assert_redirected_to workflows_path
  assert_equal session[:user_id].to_i, user.id
end

Then, leverage the helper In your controller.

Rails 5 discourages from setting the session in controller tests https://github.com/rails/rails/issues/25394#issuecomment-225728348

olimart avatar Nov 11 '16 18:11 olimart

I ran into this on updating an app from Rails 4 to Rails 5. Here's how I got through it:

  • Convert all of your controller inheritance from ActionController::TestCase to ActionDispatch::IntegrationTest

Then update your /test/test_helper.rb to include the following (making sure to replace 'secret' with whatever password you have setup in your fixtures)

class ActionDispatch::IntegrationTest
  include Sorcery::TestHelpers::Rails::Integration

   def login_user(user)
    # post the login and follow through
    post "/sessions", params: { email: user.email,
      password: 'secret' }
    follow_redirect!
   end
end

chrisdurheim avatar Feb 03 '17 06:02 chrisdurheim