I have a Ruby on Rails 5 application where I make use of a Thread variable to access the user initiating an action for my own proprietary logging. All is working fine until it comes to my automated tests.
I have a Module in lib/current.rb
:
module Current
thread_mattr_accessor :user
end
In my app/controllers/application_controller.rb
, I set the current user like this:
around_action :set_current_user
[...]
def set_current_user
Current.user = current_user
yield
ensure
Current.user = nil
end
With that, I am able to log the currently logged in user when creating/updating/deleting an entity in my application, e.g. in app/models/entity.rb
:
after_create :log_create
before_update :log_update
before_destroy :log_destroy, prepend: true
[...]
def log_destroy
ApplicationController.helpers.create_change_log([...], Current.user.id)
end
As stated above, that is working fine until I try to run my automated tests:
entity_instance = Entity.find([...])
entity_instance.destroy
This leads to the following error message:
Minitest::UnexpectedError: NoMethodError: undefined method `id' for nil:NilClass
I have assured that the error points to the non-existence of Current.user
. Does anybody have an idea why this could be the case?
Aucun commentaire:
Enregistrer un commentaire