Ehud Lev
Ehud Lev

Reputation: 2901

Ruby Rspec Mock Same instance to behave different in the first and second call

I have a code that looks like this:

def call(some_id:)
  verify_before = @verify.call(some_id)
  return verify_before if verify_before.sucess?

  did_something = @processor.call(some_id)
  return did_something unless did_something.sucess?
  
  @verify.call(some_id)
end

I would like to mock @verify so in the first time it will return sucess? = false and on the second call it will return true

What is the best approach?

Upvotes: 3

Views: 2259

Answers (1)

spickermann
spickermann

Reputation: 106882

Yes, you can tell RSpec mocks to return different values on each call.

The following example would return false on the first call and true on all later calls:

allow(@verify).to receive(:sucess?).and_return(false, true)

How to integrate this into your tests depend on how you set @verify and how your tests look like in general.

Upvotes: 9

Related Questions