我正在尝试通过混合(mixin)来注入一个after_save回调函数,但是我的rspec测试告诉我当调用create
方法时回调被调用了两次。为什么会出现这种情况?
以下rspec测试失败
it 'should call callback' do
Product.any_instance.should_receive(:update_linkable_attachments).once
Product.create(:name=>'abc')
end
失败信息如下:
Failure/Error: Unable to find matching line from backtrace
(#<Product:0xb7db738>).update_linkable_attachments(any args)
expected: 1 time
received: 2 times
这是代码
module MainModuleSupport
def self.included(base)
base.instance_eval("after_save :update_linkable_attachments")
end
def update_linkable_attachments
LinkedAttachment.delay.create_from_attachment self
end
end
class Product < ActiveRecord::Base
include MainModuleSupport
...
end
Product类还有其他代码,但没有任何其他回调函数。
LinkedAttachment.delay.create_from_attachment
是什么吗? - p.matsinopoulos