[英]Rspec stubbing method for only specific arguments
有沒有辦法只針對特定參數的存根方法。 像這樣的東西
boss.stub(:fire!).with(employee1).and_return(true)
如果任何其他員工被傳遞給boss.fire!
方法,我會讓boss received unexpected message
錯誤,但我真正想要的只是覆蓋特定參數的方法,並留給所有其他人。
有什么想法可以做到這一點?
您可以為fire!
添加默認存根fire!
調用原始實現的方法:
boss.stub(:fire!).and_call_original
boss.stub(:fire!).with(employee1).and_return(true)
Rspec 3語法(@ pk-nb)
allow(boss).to receive(:fire!).and_call_original
allow(boss).to receive(:fire!).with(employee1).and_return(true)
您可以嘗試編寫自己的存根方法,使用這樣的代碼
fire_method = boss.method(:fire!)
boss.stub!(:fire!) do |employee|
if employee == employee1
true
else
fire_method.call(*args)
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.