user93796
user93796

Reputation: 18379

Mocking a static method with different arguments and different results

I have a static class Exec with method doSomething(for)

Now i want to mock it for following 2 invocations in order or any order on by one.

Exec.stub(:doSomthing).with('a').and_return('called with a')
Exec.stub(:doSomthing).with('b').and_return('called with b')

Am getting error

Please stub a default value first if message might be received with other args as well.

How do i resolve it?

Note :This above code is just pseudocode and not my real code

Upvotes: 0

Views: 2364

Answers (1)

Greg
Greg

Reputation: 6648

You can do it this way:

allow(Exec).to receive(:doSomthing).with('a').and_return('called with a')
allow(Exec).to receive(:doSomthing).with('b').and_return('called with b')

use expect instead of allow if you need your spec to fail when the method was not called at all.

Upvotes: 3

Related Questions