phi*_*hil 12 rspec ruby-on-rails-3
我有一个有一些观察者的rails3应用程序.我不能为我的生活弄清楚如何关闭我的rspec测试!
Rob*_*idd 30
当与Rails 3.1+一起使用时,no_peeping_toms将输出弃用警告.它目前有7个拉取请求打开以删除这些弃用警告,但是Rails 3.1+不需要宝石.Rails 3.1为ActiveModel(以及ActiveRecord)添加了启用和禁用观察者的能力.
您可以在spec_helper中添加以下行来关闭所有ActiveRecord后代模型上的所有观察者:
# spec/spec_helper.rb
...
RSpec.configure do |config|
...
config.before do
...
ActiveRecord::Base.observers.disable :all # <-- Turn 'em all off!
end
end
Run Code Online (Sandbox Code Playgroud)
您可以选择性地重新启用它们,以通过使用enable方法将操作包装在规范中来测试它们的行为.
# 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3023 次 |
| 最近记录: |