假設你在一個Ruby on Rails應用程序中有一個ActiveRecord :: Observer--你如何用rSpec測試這個觀察者?你會如何在Ruby on Rails應用程序中使用rSpec測試觀察者?
回答
你在正確的跟蹤,但在使用rSpec,觀察者和模擬對象時,我遇到了一些令人沮喪的意外消息錯誤。當我對規範進行規範測試時,我不希望在消息期望中處理觀察者行爲。
在你的例子中,沒有一個真正好的方法來規範模型中的「set_status」,而不知道觀察者會對它做什麼。
因此,我喜歡用上面給出你的代碼,並使用無偷窺插件"No Peeping Toms" plugin.,我會符合規範的模式是這樣的:
describe Person do
it "should set status correctly" do
@p = Person.new(:status => "foo")
@p.set_status("bar")
@p.save
@p.status.should eql("bar")
end
end
您可以規範模型代碼,而不必擔心那裏有一個觀察員將會進來並打破你的價值。你會在這樣的person_observer_spec是單獨規格:
describe PersonObserver do
it "should clobber the status field" do
@p = mock_model(Person, :status => "foo")
@obs = PersonObserver.instance
@p.should_receive(:set_status).with("aha!")
@obs.after_save
end
end
如果你真的真的要測試的耦合模型和觀察類,你可以做這樣的:的
describe Person do
it "should register a status change with the person observer turned on" do
Person.with_observers(:person_observer) do
lambda { @p = Person.new; @p.save }.should change(@p, :status).to("aha!)
end
end
end
99%那個時候,我寧願在關閉觀察者的情況下進行spec測試。這樣就更簡單了。
聲明:我從來沒有真正做過這在生產現場,但它看起來像一個合理的方式是使用模擬對象,should_receive
和朋友,並調用觀察者方法直接
鑑於以下模型和觀察員:
class Person < ActiveRecord::Base
def set_status(new_status)
# do whatever
end
end
class PersonObserver < ActiveRecord::Observer
def after_save(person)
person.set_status("aha!")
end
end
我會寫這樣的規格(我跑了它,並把它傳遞)
我們一直在關注這個方法,它奇妙的作品 – 2010-08-19 15:41:50
no_peeping_toms現在是一個寶石,可以在這裏找到:https://github.com/patmaddox/no-peeping-toms
如果你想測試觀察者所觀察的正確型號和接收預期的通知,下面是一個使用RR的例子。
your_model.rb:
class YourModel < ActiveRecord::Base
...
end
your_model_observer.rb:
class YourModelObserver < ActiveRecord::Observer
def after_create
...
end
def custom_notification
...
end
end
your_model_observer_spec.rb:
before do
@observer = YourModelObserver.instance
@model = YourModel.new
end
it "acts on the after_create notification"
mock(@observer).after_create(@model)
@model.save!
end
it "acts on the custom notification"
mock(@observer).custom_notification(@model)
@model.send(:notify, :custom_notification)
end
- 1. 如何使用Ruby RSpec來測試添加觀察者的類?
- 2. 的Ruby on Rails應用程序測試使用RSpec和水豚
- 3. Rails 3.2.9。使用RSpec測試觀察者(遇到should_receive)
- 4. 如何在Rspec中測試時跳過觀察者after_save調用?
- 5. 如何在Ruby on Rails中使用Rspec測試鏈式方法
- 6. 如何使用Ruby on Rails構建測試版應用程序?
- 7. Ruby on Rails自定義觀察者
- 8. 如何在Ruby on Rails中測試聊天應用程序?
- 9. 如何測試觀察者?
- 10. 如何在Ruby on Rails應用程序
- 11. 無法在Ruby on Rails應用程序上運行Rspec,spork,自動測試
- 12. Rspec和rails中的觀察者
- 13. 使用RSpec在Rails應用程序中測試登錄流程
- 14. Ruby on Rails - Sphinx不會在Rspec測試中索引數據
- 15. Rspec的測試Rails應用程序
- 16. RSpec測試不使用rails應用程序數據庫配置
- 17. 調試Ruby on Rails應用程序1.9.2
- 18. Ruby on Rails - Devs的Rspec測試失敗
- 19. Ruby on Rails,Rspec測試時出錯
- 20. Ruby on Rails應用程序
- 21. Ruby on Rails中無表模型中的觀察者模式
- 22. 如何在Ruby on Rails應用程序中使用JQuery Mobile
- 23. 如何在rails 3中沒有觀察者運行rspec?
- 24. 在教程中使用rspec在rails上測試ruby的錯誤
- 25. Rails 3 + Rspec 2.0.1自動測試失敗:Ruby on Rails教程作者:M. Hartl
- 26. 爲了測試我的Ruby on Rails應用程序,你有什麼建議?
- 27. Ruby on Rails用MySQL測試
- 28. 如何用Ruby on Rails測試AJAX?
- 29. 如何在Ruby on Rails中自定義觀察者到控制器的動作
- 30. 測試我的Ruby on Rails應用程序
如果您想要測試觀察者,那麼我使用的模式是 `描述PersonOb服務器{(各個){| spec |。} Person.with_observers(:person_observer){spec.run}}}`這使得觀察者可以在PersonObserver describe塊中進行所有測試。 – roo 2013-01-21 04:02:58
這個答案似乎是一個答覆,但它不清楚什麼。這當然不是直接回答這個問題...... – 2014-05-01 16:45:21