9
我試圖通過混入注入的after_save的回調,但我的RSpec的測試告訴我,回調被調用兩次當create
方法被調用。爲什麼該方法被調用兩次?Rails的after_save的回調函數被調用多次
以下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
產品類有其他代碼,但沒有任何其他回調。
Rails的版本?我認爲在幾個版本中存在這個問題。 – tadman
我運行版本3.0.8 –
LinkedAttachment可能會在創作,如果是涉及到產品的一些方式 – charlysisto