怎么说“any_instance"?“应该接收"RSpec 中的任意次数 [英] How to say "any_instance" "should_receive" any number of times in RSpec
本文介绍了怎么说“any_instance"?“应该接收"RSpec 中的任意次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Rails 中有一个导入控制器,可以将多个包含多条记录的 csv 文件导入我的数据库.如果记录实际上是使用 RSpec 保存的,我想在 RSpec 中进行测试:
I've got an import controller in rails that imports several csv files with multiple records into my database. I would like to test in RSpec if the records are actually saved by using 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>
一个人为的控制器示例:
A contrived example of the 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 对此进行测试或有任何解决方法?
Is it possible to test this using RSpec or is there any workaround?
推荐答案
有一个新的语法:
expect_any_instance_of(Model).to receive(:save).at_least(:once)
这篇关于怎么说“any_instance"?“应该接收"RSpec 中的任意次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文