ruby-on-rails-3 – 如何在rails 3中没有观察者运行rspec?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – 如何在rails 3中没有观察者运行rspec?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个有一些观察者的rails3应用程序.我不能为我的生活找出如何把这些关闭我的rspec测试!

解决方法

与Rails 3.1一起使用时,no_peeping_toms输出废弃警告.它目前有 7 pull requests打开以删除这些弃用警告,但是 gem is not necessary with Rails 3.1+.Rails 3.1添加到ActiveModel(因此ActiveRecord)启用和 disable观察者的能力.

您可以将以下行放在spec_helper中以关闭所有ActiveRecord-descendant模型上的所有观察者:

@H_502_8@# spec/spec_helper.rb ... RSpec.configure do |config| ... config.before do ... ActiveRecord::Base.observers.disable :all # <-- Turn 'em all off! end end

您可以通过使用enable方法在您的规格中包含操作来有选择地重新启动它们来测试其行为.

@H_502_8@# spec/models/foo_observer_spec.rb describe FooObserver do subject { FooObserver.instance } it 'notices when new Foos are created' do subject.should_receive(:after_create) Foo.observers.enable :foo_observer do # <- Turn FooObserver on Foo.create('my new foo') end # <- ... and then back off end end

猜你在找的Ruby相关文章