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

后端 未结 7 1921
刺人心
刺人心 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条回答
  •  甜味超标
    2020-12-14 06:07

    Stub like this

    User.stub(:save) # Could be any class method in any class
    User.any_instance.stub(:save) { |*args| User.save(*args) }
    

    Then expect like this:

    # User.any_instance.should_receive(:save).at_least(:once)
    User.should_receive(:save).at_least(:once)
    

    This is a simplification of this gist, to use any_instance, since you don't need to proxy to the original method. Refer to that gist for other uses.

提交回复
热议问题