explorer
explorer

Reputation: 779

ruby rspec: how to make stub behave differently on each call

How to make stub in rspecs to behave differently on each call

for example,

teststub.onCall(0).stub(:testmethod).and_raise(Exception)
teststub.onCall(1).stub(:testmethod).and_return(true)

Seems like rspecs don't have onCall construct. how can i achieve this?

Upvotes: 0

Views: 51

Answers (1)

Siim Liiser
Siim Liiser

Reputation: 4368

Replace the implementation of it. Then you can do whatever you want with it. Including but not limited to behaving differently on each call.

teststub.stub(:testmethod) do
  if @already_raised
    true
  else
    @already_raised = true
    raise Exception
  end
end

Upvotes: 1

Related Questions