Skip to content

Commit 3aa0a7a

Browse files
committed
Fail when after/when are used with an incomplete expectation
1 parent 4dbe068 commit 3aa0a7a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

mock.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ function MockExpectation:andWillReturn(...)
7979
end
8080

8181
function MockExpectation:when(thunk)
82+
if #self._calls == 0 then error('incomplete expectation', 2) end
83+
8284
local function called(m, name, args)
8385
assert(#self._calls > 0, 'unexpected call')
8486
assert(self._calls[1]:functionMatches(m), 'unexpected function "' .. name .. '" called', 2)
@@ -93,6 +95,8 @@ function MockExpectation:when(thunk)
9395
end
9496

9597
function MockExpectation:after(thunk)
98+
if #self._calls == 0 then error('incomplete expectation', 2) end
99+
96100
self:when(thunk)
97101
end
98102

spec/mock_spec.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,22 @@ describe('The mock library', function()
253253
end)
254254
end)
255255

256+
it('should fail if when is not preceeded by shouldBeCalled or shouldBeCalledWith', function()
257+
shouldFailWith('incomplete expectation', function()
258+
local f = mock:mockFunction('f')
259+
260+
mock(f):when(function() end)
261+
end)
262+
end)
263+
264+
it('should fail if after is not preceeded by shouldBeCalled or shouldBeCalledWith', function()
265+
shouldFailWith('incomplete expectation', function()
266+
local f = mock:mockFunction('f')
267+
268+
mock(f):after(function() end)
269+
end)
270+
end)
271+
256272
-- ordering
257273

258274
-- allowed vs. not allowed functions on the expectation (ie: state machine for expectation)

0 commit comments

Comments
 (0)