Skip to content

Commit b9195f7

Browse files
committed
Fixed failure to check call occurred on correct mock
1 parent 247e43b commit b9195f7

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

moq.lua

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
local subscriber
22

3-
local function mock_handle(callback, thunk)
3+
local function unexpectedCall()
4+
error('unexpected mock function called')
5+
end
6+
7+
local function mockHandle(callback, thunk)
48
subscriber = callback
59
thunk()
610
subscriber = nil
711
end
812

9-
local function mock_called(m, args)
13+
local function mockCalled(m, args)
1014
return subscriber(m, args)
1115
end
1216

@@ -28,7 +32,7 @@ function ExpectedCall:new(f, args)
2832
end
2933

3034
function ExpectedCall:functionMatches(f)
31-
return _f == self.f
35+
return f == self._f
3236
end
3337

3438
function ExpectedCall:argsMatch(args)
@@ -80,7 +84,7 @@ function MockExpectation:when(thunk)
8084
return table.remove(self._calls, 1):getReturnValues()
8185
end
8286

83-
mock_handle(called, thunk)
87+
mockHandle(called, thunk)
8488

8589
assert(#self._calls == 0, 'not all calls occurred')
8690
end
@@ -126,7 +130,7 @@ local function createMockFunction()
126130
local f
127131

128132
function f(...)
129-
return mock_called(f, table.pack(...))
133+
return mockCalled(f, table.pack(...))
130134
end
131135

132136
return f
@@ -138,7 +142,7 @@ local function createMockMethod()
138142
function m(...)
139143
local args = table.pack(...)
140144
table.remove(args, 1)
141-
return mock_called(m, args)
145+
return mockCalled(m, args)
142146
end
143147

144148
return m
@@ -175,6 +179,6 @@ moq = {
175179
createMockObject = createMockObject
176180
}
177181

178-
setmetatable(moq, {__call = mock})
182+
setmetatable(moq, {__call = function(_, ...) return mock(...) end})
179183

180184
return moq

spec/moq_spec.lua

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,32 @@ describe('The mock library', function()
1515
end)
1616

1717
it('should alert you when a function is not called', function()
18-
shouldFail(function()
19-
local m = moq.createMockFunction()
18+
local m = moq.createMockFunction()
2019

20+
shouldFail(function()
2121
moq(m):shouldBeCalled():
2222
when(function() end)
2323
end)
2424
end)
2525

26+
it('should alert you when the wrong function is called', function()
27+
local m1 = moq.createMockFunction()
28+
local m2 = moq.createMockFunction()
29+
30+
shouldFail(function()
31+
moq(m1):shouldBeCalled():
32+
when(function() m2() end)
33+
end)
34+
end)
35+
36+
it('should alert you when a function is called unexpectedly', function()
37+
local m = moq.createMockFunction()
38+
39+
shouldFail(function()
40+
mo()
41+
end)
42+
end)
43+
2644
it('should allow you to verify that a function has been called with the correct arguments', function()
2745
local m = moq.createMockFunction()
2846

@@ -31,9 +49,9 @@ describe('The mock library', function()
3149
end)
3250

3351
it('should alert you when a function has been called with incorrect arguments', function()
34-
shouldFail(function()
35-
local m = moq.createMockFunction()
52+
local m = moq.createMockFunction()
3653

54+
shouldFail(function()
3755
moq(m):shouldBeCalledWith(1, '2'):
3856
when(function() m(1, '3') end)
3957
end)

0 commit comments

Comments
 (0)