Test helpers do not work in Rails 5
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
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 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.
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
It seems the integration test helpers only work with Rack::Tests. Neither it works for Rails integration tests nor Capybara feature tests.
Today I got the same problem.
Error:
GroupsControllerTest#test_should_get_index:
NameError: undefined local variable or method `page' for #<GroupsControllerTest:0x007fcd0dc511d8>
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.
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
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::TestCasetoActionDispatch::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