12

I am using the version of minitest with Ruby 1.9.3 How do I test for multiple invocations of a mock with it? I need something like

mockObject.expect.times(2) :method, [return_1 first time, return_2 second time] 
mockObject.verify

Is there a way to achieve this?

user949110
  • 543
  • 7
  • 20

2 Answers2

10

You need to call expect for each time the method is to be called.

mockObject.expect :method, return_1, [first, time, args]
mockObject.expect :method, return_2, [second, time, args]

# run your code using the mock object

mockObject.verify
blowmage
  • 8,854
  • 2
  • 35
  • 40
  • 1
    this does not work actually. I get a `MockExpectationError` which says `expected{:retval => return_2, :args => [second, time, args]}, got [{:retval => return_1, :args => [first, time, args]}, {:retval => return_2, :args => [second, time, args]}]` From what I understand, if I'm mocking the same method multiple times, the last 'expect' is winning and the rest are ignored. Maybe something wrong with the minitest version in Ruby 1.9.3? – user949110 Nov 15 '14 at 20:23
3

Unfortunately my minitest version is older. This has only been supported since this commit. https://github.com/seattlerb/minitest/commit/7a6f1818877c6c8a83a0e6680d5fc23ec32fa633#diff-00a18d559ad9a1e984068e38e5009047

user949110
  • 543
  • 7
  • 20