view教程 - 您将如何在Ruby on Rails应用程序中使用rSpec测试观察者?
ruby单元测试 (3)
no_peeping_toms现在是一个宝石,可以在这里找到: https://github.com/patmaddox/no-peeping-toms : https://github.com/patmaddox/no-peeping-toms
假设您的Ruby on Rails应用程序中有一个ActiveRecord :: Observer - 如何使用rSpec测试此观察者?
免责声明:我从来没有在生产网站上实际做过这个,但看起来合理的方式是使用模拟对象, 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
我会写一个这样的规格(我跑了,它通过了)
describe PersonObserver do
before :each do
@person = stub_model(Person)
@observer = PersonObserver.instance
end
it "should invoke after_save on the observed object" do
@person.should_receive(:set_status).with("aha!")
@observer.after_save(@person)
end
end
您处于正确的轨道上,但在使用rSpec,观察者和模拟对象时,我遇到了许多令人沮丧的意外消息错误。 当我对我的模型进行规范测试时,我不想在我的消息期望中处理观察者行为。
在您的示例中,没有一种非常好的方法来在模型上指定“set_status”,而不知道观察者将对它做什么。
因此,我喜欢使用“No Peeping Toms”插件。 鉴于上面的代码并使用No Peeping Toms插件,我会像这样指定模型:
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
如果你真的想要测试耦合的Model和Observer类,你可以这样做:
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%的时间,我宁愿在关闭观察员的情况下进行规范测试。 这样更简单。