如何禁用缓存清理程序以进行测试
我正在使用扫地机来观看模型并使用 snogmetrics 进行一些日志记录:
class UserSweeper < ActionController::Caching::Sweeper
observe User
def after_create(user)
km.identify(user.email)
end
end
我使用扫地机而不是观察者来执行此操作,因为 snogmetrics 需要会话才能正常运行。
然而,在测试时,snogmetrics 完全失败了
NoMethodError: undefined method `identify' for nil:NilClass
如何禁用单元测试的扫描器?谢谢!
I'm using a sweeper to watch a model and do some logging with snogmetrics:
class UserSweeper < ActionController::Caching::Sweeper
observe User
def after_create(user)
km.identify(user.email)
end
end
I'm doing this with a sweeper instead of an observer because snogmetrics needs the session in order to function properly.
However, while testing, snogmetrics utterly fails
NoMethodError: undefined method `identify' for nil:NilClass
How can I disable the sweeper for unit tests? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
km
在哪里定义的?据我所知,它没有在您的 user_sweeper 中定义,因此无论您所处的环境如何,它都会失败。Where is
km
defined? From what I see, it's not defined in your user_sweeper so it will fail regardless the environment you're in.我能够在测试期间使用 no-peeping-toms gem 来阻止观察者。
注意:这也适用于 rake 任务。我在迁移过程中遇到了观察员被调用的问题,这解决了它。
I was able to use the no-peeping-toms gem to block observers during testing.
NOTE: This also works for rake tasks. I had an issue with observers being called during a migration and this solved it.