Skip to content

Commit ae96f62

Browse files
committed
Added tests (which exposed a bug\!) for mixing ordered and unordered expectations
1 parent 9836d36 commit ae96f62

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

mock.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ function MockExpectation:when(thunk)
9898
validFunctionFound = true
9999

100100
if call:argsMatch(args) then
101+
if call:hasFixedOrder() and i > 1 then
102+
self._calls[i - 1]:fixOrder()
103+
end
104+
101105
return table.remove(self._calls, i):getReturnValues()
102106
end
103107
end

spec/mock_spec.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,27 +255,31 @@ describe('The mock library', function()
255255
it('should fail if when is not preceeded by shouldBeCalled or shouldBeCalledWith', function()
256256
shouldFailWith('incomplete expectation', function()
257257
local f = mock:mockFunction('f')
258+
258259
mock(f):when(function() end)
259260
end)
260261
end)
261262

262263
it('should fail if after is not preceeded by shouldBeCalled or shouldBeCalledWith', function()
263264
shouldFailWith('incomplete expectation', function()
264265
local f = mock:mockFunction('f')
266+
265267
mock(f):after(function() end)
266268
end)
267269
end)
268270

269271
it('should fail if shouldBeCalled is used after a call has already been specified', function()
270272
shouldFailWith('call already specified', function()
271273
local f = mock:mockFunction('f')
274+
272275
mock(f):shouldBeCalled():shouldBeCalled()
273276
end)
274277
end)
275278

276279
it('should fail if shouldBeCalled is used after a call has already been specified', function()
277280
shouldFailWith('call already specified', function()
278281
local f = mock:mockFunction('f')
282+
279283
mock(f):shouldBeCalled():shouldBeCalledWith()
280284
end)
281285
end)
@@ -321,4 +325,38 @@ describe('The mock library', function()
321325
end)
322326
end)
323327
end)
328+
329+
it('should catch out of order calls when mixed with unordered calls', function()
330+
local f1 = mock:mockFunction('f1')
331+
local f2 = mock:mockFunction('f2')
332+
local f3 = mock:mockFunction('f3')
333+
334+
shouldFailWith('unexpected function "f3" called', function()
335+
mock(f1):shouldBeCalled():
336+
andAlso(mock(f2):shouldBeCalled()):
337+
andThen(mock(f3):shouldBeCalled()):
338+
when(function()
339+
f2()
340+
f3()
341+
f1()
342+
end)
343+
end)
344+
end)
345+
346+
it('should allow ordered and unordered calls to be mixed', function()
347+
local f = mock:mockFunction('f')
348+
349+
mock(f):shouldBeCalledWith(1):
350+
andAlso(mock(f):shouldBeCalledWith(2)):
351+
andThen(mock(f):shouldBeCalledWith(3)):
352+
andAlso(mock(f):shouldBeCalledWith(4)):
353+
when(function()
354+
f(2)
355+
f(1)
356+
f(4)
357+
f(3)
358+
end)
359+
end)
360+
361+
-- may be called? ie: calls that are not required
324362
end)

0 commit comments

Comments
 (0)