Rspec stubbing method for only specific arguments

前端 未结 2 1135
野的像风
野的像风 2021-02-02 06:34

Is there a way to stub method for only specific arguments. Something like this

boss.stub(:fire!).with(employee1).and_return(true)

If any other

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 06:49

    You can add a default stub for the fire! method which will call original implementation:

    boss.stub(:fire!).and_call_original
    boss.stub(:fire!).with(employee1).and_return(true)
    

    Rspec 3 Syntax (@pk-nb)

    allow(boss).to receive(:fire!).and_call_original
    allow(boss).to receive(:fire!).with(employee1).and_return(true)
    

提交回复
热议问题