怎么说"any_instance" "should_receive" RSpec中的任何次数 [英] How to say "any_instance" "should_receive" any number of times in RSpec

查看:54
本文介绍了怎么说"any_instance" "should_receive" 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>

人为设计的示例:

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" "should_receive" RSpec中的任何次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆