我在rails中有一个导入 Controller ,该 Controller 将具有多个记录的多个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>
人为的 Controller 示例:
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对此进行测试,或者有任何解决方法?
最佳答案
有一个新的语法:
expect_any_instance_of(Model).to receive(:save).at_least(:once)