1
1
describe (' The mock library' , function ()
2
2
moq = require ' moq'
3
3
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 ()
5
78
local m1 = moq .createMockFunction ()
6
79
local m2 = moq .createMockFunction ()
7
80
@@ -27,19 +100,13 @@ describe('The mock library', function()
27
100
return moq .mock (m2 ):shouldBeCalledWith (1 , 2 , 3 )
28
101
end
29
102
30
- function yetAnotherThingShouldHappen ()
31
- return moq .mock (m2 ):shouldBeCalledWith (1 ):andWillReturn (4 )
32
- end
33
-
34
103
function codeUnderTestRuns ()
35
104
m1 ()
36
105
m2 (1 , 2 , 3 )
37
- assert .is .equal (m2 (1 ), 4 )
38
106
end
39
107
40
108
somethingShouldHappen ():
41
109
andAlso (anotherThingShouldHappen ()):
42
- andThen (yetAnotherThingShouldHappen ()):
43
110
when (codeUnderTestRuns )
44
111
end )
45
112
@@ -52,27 +119,58 @@ describe('The mock library', function()
52
119
mockedTable = moq .createMockTable (someTable )
53
120
54
121
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' )
56
141
end )
57
142
58
143
it (' should allow an object with methods to be mocked' , function ()
59
144
local someObject = {}
60
145
61
- function someObject :foo ()
62
- end
63
-
64
- function someObject :bar ()
65
- end
146
+ function someObject :foo () end
147
+ function someObject :bar () end
66
148
67
149
mockedObject = moq .createMockObject (someObject )
68
150
69
151
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 )
71
157
end )
72
158
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 )
74
172
75
173
-- ordering
76
174
77
- -- allowed vs. not allowed functions on the expectation
175
+ -- allowed vs. not allowed functions on the expectation (ie: state machine for expectation)
78
176
end )
0 commit comments