How to say “any_instance” “should_receive” any number of times in RSpec

后端 未结 7 1920
刺人心
刺人心 2020-12-14 05:24

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 save

7条回答
  •  猫巷女王i
    2020-12-14 06:05

    This is Rob's example using RSpec 3.3, which no longer supports Foo.any_instance. I found this useful when in a loop creating objects

    # code (simplified version)
    array_of_hashes.each { |hash| Model.new(hash).write! }
    
    # spec
    it "calls write! for each instance of Model" do 
      call_count = 0
      allow_any_instance_of(Model).to receive(:write!) { call_count += 1 }
    
      response.process # run the test
      expect(call_count).to eq(2)
    end
    

提交回复
热议问题