Skip to content

Commit 4dbe068

Browse files
committed
Disallow andWillReturn from being called when there is no call
1 parent 1672f79 commit 4dbe068

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mock.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ function ExpectedCall:getReturnValues(...)
5454
end
5555

5656

57-
5857
MockExpectation = {}
5958

6059
function MockExpectation:new(m)
@@ -70,7 +69,12 @@ function MockExpectation:new(m)
7069
end
7170

7271
function MockExpectation:andWillReturn(...)
72+
if #self._calls == 0 then
73+
error('cannot set return value for an unspecified call', 2)
74+
end
75+
7376
self._calls[#self._calls]:setReturnValues(...)
77+
7478
return self
7579
end
7680

spec/mock_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ describe('The mock library', function()
245245
end)
246246
end)
247247

248+
it('should fail if andWillReturn is not preceeded by shouldBeCalled or shouldBeCalledWith', function()
249+
shouldFailWith('cannot set return value for an unspecified call', function()
250+
local f = mock:mockFunction('f')
251+
252+
mock(f):andWillReturn(1)
253+
end)
254+
end)
255+
248256
-- ordering
249257

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

0 commit comments

Comments
 (0)