Skip to content

Commit 247e43b

Browse files
committed
Made the mock table callable
1 parent d7a1cd0 commit 247e43b

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

moq.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,13 @@ local function createMockObject(o)
168168
return mocked
169169
end
170170

171-
return {
172-
mock = mock,
171+
moq = {
173172
createMockFunction = createMockFunction,
174173
createMockTable = createMockTable,
175174
createMockMethod = createMockMethod,
176175
createMockObject = createMockObject
177176
}
177+
178+
setmetatable(moq, {__call = mock})
179+
180+
return moq

spec/moq_spec.lua

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,39 @@ describe('The mock library', function()
1010
it('should allow you to verify that a function is called', function()
1111
local m = moq.createMockFunction()
1212

13-
moq.mock(m):shouldBeCalled():
13+
moq(m):shouldBeCalled():
1414
when(function() m() end)
1515
end)
1616

1717
it('should alert you when a function is not called', function()
1818
shouldFail(function()
1919
local m = moq.createMockFunction()
2020

21-
moq.mock(m):shouldBeCalled():
21+
moq(m):shouldBeCalled():
2222
when(function() end)
2323
end)
2424
end)
2525

2626
it('should allow you to verify that a function has been called with the correct arguments', function()
2727
local m = moq.createMockFunction()
2828

29-
moq.mock(m):shouldBeCalledWith(1, '2'):
29+
moq(m):shouldBeCalledWith(1, '2'):
3030
when(function() m(1, '2') end)
3131
end)
3232

3333
it('should alert you when a function has been called with incorrect arguments', function()
3434
shouldFail(function()
3535
local m = moq.createMockFunction()
3636

37-
moq.mock(m):shouldBeCalledWith(1, '2'):
37+
moq(m):shouldBeCalledWith(1, '2'):
3838
when(function() m(1, '3') end)
3939
end)
4040
end)
4141

4242
it('should allow you to specify the return value of a mocked function', function()
4343
local m = moq.createMockFunction()
4444

45-
moq.mock(m):shouldBeCalled():andWillReturn(4):
45+
moq(m):shouldBeCalled():andWillReturn(4):
4646
when(function()
4747
assert.is.equal(m(), 4)
4848
end)
@@ -51,7 +51,7 @@ describe('The mock library', function()
5151
it('should allow you to specify multiple return values for a mocked function', function()
5252
local m = moq.createMockFunction()
5353

54-
moq.mock(m):shouldBeCalled():andWillReturn(1, 2):
54+
moq(m):shouldBeCalled():andWillReturn(1, 2):
5555
when(function()
5656
r1, r2 = m()
5757
assert.is.equal(r1, 1)
@@ -62,8 +62,8 @@ describe('The mock library', function()
6262
it('should allow you to check that a function has been called multiple times', function()
6363
local m = moq.createMockFunction()
6464

65-
moq.mock(m):shouldBeCalled():
66-
andAlso(moq.mock(m):shouldBeCalledWith(1, 2, 3)):
65+
moq(m):shouldBeCalled():
66+
andAlso(moq(m):shouldBeCalledWith(1, 2, 3)):
6767
when(function()
6868
m()
6969
m(1, 2, 3)
@@ -74,8 +74,8 @@ describe('The mock library', function()
7474
local m1 = moq.createMockFunction()
7575
local m2 = moq.createMockFunction()
7676

77-
moq.mock(m1):shouldBeCalled():
78-
andAlso(moq.mock(m2):shouldBeCalledWith(1, 2, 3)):
77+
moq(m1):shouldBeCalled():
78+
andAlso(moq(m2):shouldBeCalledWith(1, 2, 3)):
7979
when(function()
8080
m1()
8181
m2(1, 2, 3)
@@ -86,9 +86,9 @@ describe('The mock library', function()
8686
local m1 = moq.createMockFunction()
8787
local m2 = moq.createMockFunction()
8888

89-
moq.mock(m1):shouldBeCalled():
90-
andAlso(moq.mock(m2):shouldBeCalledWith(1, 2, 3)):
91-
andThen(moq.mock(m2):shouldBeCalledWith(1):andWillReturn(4)):
89+
moq(m1):shouldBeCalled():
90+
andAlso(moq(m2):shouldBeCalledWith(1, 2, 3)):
91+
andThen(moq(m2):shouldBeCalledWith(1):andWillReturn(4)):
9292
when(function()
9393
m1()
9494
m2(1, 2, 3)
@@ -101,11 +101,11 @@ describe('The mock library', function()
101101
local m2 = moq.createMockFunction()
102102

103103
function somethingShouldHappen()
104-
return moq.mock(m1):shouldBeCalled()
104+
return moq(m1):shouldBeCalled()
105105
end
106106

107107
function anotherThingShouldHappen()
108-
return moq.mock(m2):shouldBeCalledWith(1, 2, 3)
108+
return moq(m2):shouldBeCalledWith(1, 2, 3)
109109
end
110110

111111
function codeUnderTestRuns()
@@ -126,8 +126,8 @@ describe('The mock library', function()
126126

127127
mockedTable = moq.createMockTable(someTable)
128128

129-
moq.mock(mockedTable.foo):shouldBeCalledWith(1):andWillReturn(2):
130-
andAlso(moq.mock(mockedTable.bar):shouldBeCalled()):
129+
moq(mockedTable.foo):shouldBeCalledWith(1):andWillReturn(2):
130+
andAlso(moq(mockedTable.bar):shouldBeCalled()):
131131
when(function()
132132
mockedTable.foo(1)
133133
mockedTable.bar()
@@ -142,7 +142,7 @@ describe('The mock library', function()
142142

143143
mockedTable = moq.createMockTable(someTable)
144144

145-
moq.mock(mockedTable.foo):shouldBeCalledWith(1):andWillReturn(2):
145+
moq(mockedTable.foo):shouldBeCalledWith(1):andWillReturn(2):
146146
when(function()
147147
mockedTable:foo(1)
148148
end)
@@ -157,8 +157,8 @@ describe('The mock library', function()
157157

158158
mockedObject = moq.createMockObject(someObject)
159159

160-
moq.mock(mockedObject.foo):shouldBeCalledWith(1):andWillReturn(2):
161-
andAlso(moq.mock(mockedObject.bar):shouldBeCalled()):
160+
moq(mockedObject.foo):shouldBeCalledWith(1):andWillReturn(2):
161+
andAlso(moq(mockedObject.bar):shouldBeCalled()):
162162
when(function()
163163
mockedObject:foo(1)
164164
mockedObject:bar()
@@ -173,7 +173,7 @@ describe('The mock library', function()
173173

174174
mockedObject = moq.createMockObject(someObject)
175175

176-
moq.mock(mockedObject.foo):shouldBeCalledWith(1):andWillReturn(2):
176+
moq(mockedObject.foo):shouldBeCalledWith(1):andWillReturn(2):
177177
when(function()
178178
mockedObject.foo(1)
179179
end)

0 commit comments

Comments
 (0)