Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stub unlimited times in RSpec

RSpec allows stubbing a potential request as follows -

expect(@project).to receive(:msg).at_least(n).times

What about situations where I don't know (and don't care) how many times @project receives :msg.

Is there an "unlimited" stub in RSpec that tells it to always stub it for that example no matter how many times its called?

like image 769
user2490003 Avatar asked Oct 29 '25 15:10

user2490003


2 Answers

You can use allow if you don't care how many times it gets stubbed.

allow(@project).to receive(:msg)
like image 95
Huy Avatar answered Oct 31 '25 07:10

Huy


Using allow(@project).to receive(:msg) (as suggested by @Huy) or @project.stub(:msg) will stub the method.

Doing expect(@project).to receive(:msg) just allows you to make an assertion that the stub was called as many times as you expected.

If you don't make an assertion about it, or make your assertion general (e.g. @project.should_receive(:msg).at_least(:once)), then it doesn't matter if the method was called more than that.

You can find more info in the RSpec docs:

Method Stubs

Expectations

like image 43
dinjas Avatar answered Oct 31 '25 07:10

dinjas