Set current_user in Controller Test with Rails 5 Minitest
Asked Answered
B

0

6

Anyone know how to set the devise helper function #current_user in a controller test (using fixture data) for a Rails 5 app? I've tried the following:

  • sign_in users(:example_user)
  • @current_user = users(:example_user)
  • current_user = users(:example_user)
  • @controller.stubs(:current_user).returns(users(:example_user)).

I'm writing tests for the paper_trail gem, which uses #current_user to set the 'whodunnit' attribute (i.e. id of current_user) on the version.

Biamonte answered 24/5, 2017 at 18:23 Comment(3)
Don't use controller tests for a new app as they are depreciated. Instead use ActionDispatch::IntegrationTest as this is a more future proof way forward. An added bonus is you can simply use include Warden::Test::Helpers and login users with login_as @userYogh
I missed the part about writing it for the paper_trail gem. But my advice still stands. Convert it to an ActionDispatch::IntegrationTest and stop poking around in the internals.Yogh
Max, I think your comment could be an answer. I use login_as on my paper_trail projects. To the OP: re: paper_trail, make sure to read section 4.a. Finding Out Who Was Responsible For A ChangeAdeline

© 2022 - 2024 — McMap. All rights reserved.