我有一个基于redis的新闻订阅源,当某些事件发生时,通过回调获取项目。例如,当用户在一本书上留言时,它会被插入到本书读者的新闻中。使用lambda测试rails中的回调?
class Note < ActiveRecord::Base
after_save do |note|
notify_the note.book.readers
end
...
end
现在这很好,我99%肯定它的工作原理,因为我可以看我的饲料,看看那里的笔记。我的问题是用最新的rspec-rails在Rails 3上测试它。
出于某种原因,这个通行证:
spec/models/note_spec.rb
describe "note creation" do
it "should notify the readers of the book the note is on" do
@user.feed.count.should == 0
@note.save!
@user.feed.count.should == 1
end
end
但这并不:
spec/models/note_spec.rb
describe "note creation" do
it "should notify the readers of the book the note is on" do
lambda do
@note.save!
end.should change(@user.feed, :count).by(1)
end
end
,我想不出有什么区别?
我唯一能想到的就是`@ note.save!`抛出了一个异常,并且它不会因为它在lambda中被隔离而冒泡。但是,如果第二次测试中的“@ note”与第一次测试中的“完全相同”,那不太可能。 – 2011-04-15 19:22:40
是的,他们肯定是一样的。 – 2011-04-16 01:25:34