Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test helpers do not work in Rails 5 #775

Open
5t111111 opened this issue May 8, 2016 · 8 comments
Open

Test helpers do not work in Rails 5 #775

5t111111 opened this issue May 8, 2016 · 8 comments

Comments

@5t111111
Copy link

5t111111 commented May 8, 2016

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. heartcombo/devise#3913

@arnvald
Copy link
Collaborator

arnvald commented May 15, 2016

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.

@5t111111
Copy link
Author

@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.

@atstockland
Copy link

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

@dnesteryuk
Copy link

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

@calazans10
Copy link

Today I got the same problem.

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

@olimart
Copy link

olimart commented Nov 11, 2016

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
Copy link

olimart commented Nov 11, 2016

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
rails/rails#25394 (comment)

@chrisdurheim
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants