I've created an observer that observers all models for changes to the database. This auditor observer works great in and of itself, but it destroys the testing suite. I'm unclear as to why the observer causes all my tests to fail. If you know, please tell me. (Incidentally, I'm running Rails 3.2.14.)

But more importantly, I'm looking for a way to get this observer to play nicely with the testing suite. I was thinking of adding some sort of flag on the environment configuration layer that the observer class can evaluate at run time. Then I would just set the flag to 'false' for the dev and test environments and true for production. But I have no idea how I would go about doing this. Any suggestions?

I don't have the option of disabling the observer in my spec_helper because a substantial quantity of tests don't actually load the spec_helper. Most of these tests don't actually make changes to the database, but hypothetically, they could and I don't want to have to dig through hundreds of tests files to refactor them.

So, I'm looking for a way to disable or incapacitate my observer for the entire test environment. The configuration layer seems like the appropriate place to do this, but I'm not sure how to go about it. I'd love to hear any advice on how to accomplish this.

Update: related post with more detailed account of the issues I'm having: https://stackoverflow.com/questions/18321693/how-to-disable-an-activerecord-observer-in-a-rails-engine?noredirect=1#comment26888767_18321693

有帮助吗?

解决方案

I'd say:

  • either you declare the observer in all config files but the test one

  • or you load it conditionally: config.active_record.observers = your_list if Rails.env.test?

Btw, it seems weird you do not need to spec it too. Anyway, I know you can't sum up your app logic there, just curious.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top