我有一个导入控制器在轨道,导入多个 csv 文件与多个记录到我的数据库。我想在 RSpec 中测试记录是否是通过使用 RSpec 保存的:
<Model>.any_instance.should_receive(:save).at_least(:once)
然而,我得到的错误说:
The message 'save' was received by <model instance> but has already been received by <another model instance>
一个人为设计的控制器例子:
rows = CSV.parse(uploaded_file.tempfile, col_sep: "|")
ActiveRecord::Base.transaction do
rows.each do |row|
mutation = Mutation.new
row.each_with_index do |value, index|
Mutation.send("#{attribute_order[index]}=", value)
end
mutation.save
end
有没有可能使用 RSpec 来测试它,或者有没有什么变通方法?