Ruby on rails 在rails中使用lambda测试回调?
我有一个基于redis的新闻提要,当某些事件发生时,它通过回调将项目插入其中。例如,当用户在一本书上做笔记时,它会被插入到该书读者的新闻提要中Ruby on rails 在rails中使用lambda测试回调?,ruby-on-rails,ruby-on-rails-3,rspec,redis,rspec2,Ruby On Rails,Ruby On Rails 3,Rspec,Redis,Rspec2,我有一个基于redis的新闻提要,当某些事件发生时,它通过回调将项目插入其中。例如,当用户在一本书上做笔记时,它会被插入到该书读者的新闻提要中 class Note < ActiveRecord::Base after_save do |note| notify_the note.book.readers end ... end 但这并不是: spec/models/note_spec.rb describe "note creation" do it "shoul
class Note < ActiveRecord::Base
after_save do |note|
notify_the note.book.readers
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
我不知道区别是什么?RSpec不支持此匹配器的do/end语法。请参阅本页底部的警告 为了使用lambda并测试更改,您需要使用大括号{}——do/end语法还不受支持。我唯一能想到的是
@note.save
正在引发异常,它不会冒泡,因为它在lambda中是孤立的。但是如果第二个测试中的@note
与第一个测试中的完全相同,那就不太可能了。是的,它们肯定是一样的。我会跟踪测试日志并运行这两个测试,看看实际运行的是什么。一定有不同的地方。抱歉,这是我自己的错,但我应该说我使用的是rpec2(afaik确实支持lambda的do..end语法。我用大括号尝试了它,但似乎没有解决问题。我尝试了expect{}.to,但也失败了。查看代码,RSpec 2似乎不支持do…end
。请参阅lib/spec/matchers/change.rb中的
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