Skip to content

Commit 02cf149

Browse files
committed
Added more and better tests
1 parent 21967e8 commit 02cf149

File tree

1 file changed

+114
-16
lines changed

1 file changed

+114
-16
lines changed

spec/moq_spec.lua

Lines changed: 114 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,80 @@
11
describe('The mock library', function()
22
moq = require 'moq'
33

4-
it('should work', function()
4+
it('should allow you to verify that a function is called', function()
5+
local m = moq.createMockFunction()
6+
7+
moq.mock(m):shouldBeCalled():
8+
when(function() m() end)
9+
end)
10+
11+
it('should alert you when a function is not called', function()
12+
-- local m = moq.createMockFunction()
13+
14+
-- moq.mock(m):shouldBeCalled():
15+
-- when(function() m() end)
16+
error('todo')
17+
end)
18+
19+
it('should allow you to verify that a function has been called with the correct arguments', function()
20+
local m = moq.createMockFunction()
21+
22+
moq.mock(m):shouldBeCalledWith(1, '2'):
23+
when(function() m(1, '2') end)
24+
end)
25+
26+
it('should alert you when a function has been called with incorrect arguments', function()
27+
-- local m = moq.createMockFunction()
28+
29+
-- moq.mock(m):shouldBeCalledWith(1, '2'):
30+
-- when(function() m(1, '2') end)
31+
error('todo')
32+
end)
33+
34+
it('should allow you to specify the return value of a mocked function', function()
35+
local m = moq.createMockFunction()
36+
37+
moq.mock(m):shouldBeCalled():andWillReturn(4):
38+
when(function()
39+
assert.is.equal(m(), 4)
40+
end)
41+
end)
42+
43+
it('should allow you to specify multiple return values for a mocked function', function()
44+
local m = moq.createMockFunction()
45+
46+
moq.mock(m):shouldBeCalled():andWillReturn(1, 2):
47+
when(function()
48+
r1, r2 = m()
49+
assert.is.equal(r1, 1)
50+
assert.is.equal(r2, 2)
51+
end)
52+
end)
53+
54+
it('should allow you to check that a function has been called multiple times', function()
55+
local m = moq.createMockFunction()
56+
57+
moq.mock(m):shouldBeCalled():
58+
andAlso(moq.mock(m):shouldBeCalledWith(1, 2, 3)):
59+
when(function()
60+
m()
61+
m(1, 2, 3)
62+
end)
63+
end)
64+
65+
it('should allow you to check that multiple functions are called', function()
66+
local m1 = moq.createMockFunction()
67+
local m2 = moq.createMockFunction()
68+
69+
moq.mock(m1):shouldBeCalled():
70+
andAlso(moq.mock(m2):shouldBeCalledWith(1, 2, 3)):
71+
when(function()
72+
m1()
73+
m2(1, 2, 3)
74+
end)
75+
end)
76+
77+
it('should allow you to mix and match call types', function()
578
local m1 = moq.createMockFunction()
679
local m2 = moq.createMockFunction()
780

@@ -27,19 +100,13 @@ describe('The mock library', function()
27100
return moq.mock(m2):shouldBeCalledWith(1, 2, 3)
28101
end
29102

30-
function yetAnotherThingShouldHappen()
31-
return moq.mock(m2):shouldBeCalledWith(1):andWillReturn(4)
32-
end
33-
34103
function codeUnderTestRuns()
35104
m1()
36105
m2(1, 2, 3)
37-
assert.is.equal(m2(1), 4)
38106
end
39107

40108
somethingShouldHappen():
41109
andAlso(anotherThingShouldHappen()):
42-
andThen(yetAnotherThingShouldHappen()):
43110
when(codeUnderTestRuns)
44111
end)
45112

@@ -52,27 +119,58 @@ describe('The mock library', function()
52119
mockedTable = moq.createMockTable(someTable)
53120

54121
moq.mock(mockedTable.foo):shouldBeCalledWith(1):andWillReturn(2):
55-
when(function() mockedTable.foo(1) end)
122+
andAlso(moq.mock(mockedTable.bar):shouldBeCalled()):
123+
when(function()
124+
mockedTable.foo(1)
125+
mockedTable.bar()
126+
end)
127+
end)
128+
129+
it('should fail when a function is incorrectly used as a method', function()
130+
-- local someTable = {
131+
-- foo = function() end
132+
-- }
133+
134+
-- mockedTable = moq.createMockTable(someTable)
135+
136+
-- moq.mock(mockedTable.foo):shouldBeCalledWith(1):andWillReturn(2):
137+
-- when(function()
138+
-- mockedTable:foo(1)
139+
-- end
140+
error('todo')
56141
end)
57142

58143
it('should allow an object with methods to be mocked', function()
59144
local someObject = {}
60145

61-
function someObject:foo()
62-
end
63-
64-
function someObject:bar()
65-
end
146+
function someObject:foo() end
147+
function someObject:bar() end
66148

67149
mockedObject = moq.createMockObject(someObject)
68150

69151
moq.mock(mockedObject.foo):shouldBeCalledWith(1):andWillReturn(2):
70-
when(function() mockedObject:foo(1) end)
152+
andAlso(moq.mock(mockedObject.bar):shouldBeCalled()):
153+
when(function()
154+
mockedObject:foo(1)
155+
mockedObject:bar()
156+
end)
71157
end)
72158

73-
-- multiple return values
159+
it('should fail when a method is incorrectly used as a function', function()
160+
-- local someObject = {}
161+
162+
-- function someObject:foo() end
163+
164+
-- mockedObject = moq.createMockObject(someObject)
165+
166+
-- moq.mock(mockedObject.foo):shouldBeCalledWith(1):andWillReturn(2):
167+
-- when(function()
168+
-- mockedObject.foo(1)
169+
-- end)
170+
error('todo')
171+
end)
74172

75173
-- ordering
76174

77-
-- allowed vs. not allowed functions on the expectation
175+
-- allowed vs. not allowed functions on the expectation (ie: state machine for expectation)
78176
end)

0 commit comments

Comments
 (0)