Search code examples
ruby-on-railsrubyrspectestingacts-as-audited

Ruby on Rails: RSpec and Acts As Audited (disabling acts_as_audited while testing?)


I've been getting an error, which I think could be solved by disabling acts_as_audited when running tests, or at least stubbing current_user in audit sweeper. The error is below. What do you think I should do?

NoMethodError in 'Order should create a new instance given valid attributes'
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.current_user
/home/ramon/rails/app/vendor/plugins/acts_as_audited/lib/acts_as_audited/audit_sweeper.rb:73:in `send'
/home/ramon/rails/app/vendor/plugins/acts_as_audited/lib/acts_as_audited/audit_sweeper.rb:73:in `current_user'
/home/ramon/rails/app/vendor/plugins/acts_as_audited/lib/acts_as_audited/audit_sweeper.rb:61:in `after_create'
./spec/models/order_spec.rb:13:

In spec/models/order_spec.rb I've tried stubbing AuditSweeper.current_user, template.current_user, and controller.current_user to return a mock User model. None work.

Any help is appreciated. Thanks!


Solution

  • it looks as though one of the objects current_user is being called on is nil. so stubbing current user won't work. you need to track down the nil object and make sure it is initialized.