@@ -7,128 +7,139 @@ describe('The mock library', function()
7
7
end
8
8
end
9
9
10
+ local function shouldFailWith (expectedMessage , test )
11
+ local result , actualMessage = pcall (test )
12
+ _ , _ , actualMessage = actualMessage :find (" :%w+: (.+)" )
13
+
14
+ if (result ) then
15
+ error (' expected failure did not occur' )
16
+ elseif (actualMessage ~= expectedMessage ) then
17
+ error (' expected failure message: "' .. expectedMessage .. ' " did not match actual failure message: "' .. actualMessage .. ' "' )
18
+ end
19
+ end
20
+
10
21
it (' should allow you to verify that a function is called' , function ()
11
- local m = mock :mockFunction ()
22
+ local f = mock :mockFunction (' f ' )
12
23
13
- mock (m ):shouldBeCalled ():
14
- when (function () m () end )
24
+ mock (f ):shouldBeCalled ():
25
+ when (function () f () end )
15
26
end )
16
27
17
28
it (' should alert you when a function is not called' , function ()
18
- local m = mock :mockFunction ()
29
+ local f = mock :mockFunction (' f ' )
19
30
20
31
shouldFail (function ()
21
- mock (m ):shouldBeCalled ():
32
+ mock (f ):shouldBeCalled ():
22
33
when (function () end )
23
34
end )
24
35
end )
25
36
26
37
it (' should alert you when the wrong function is called' , function ()
27
- local m1 = mock :mockFunction ()
28
- local m2 = mock :mockFunction ()
38
+ local f1 = mock :mockFunction (' f1 ' )
39
+ local f2 = mock :mockFunction (' f2 ' )
29
40
30
- shouldFail ( function ()
31
- mock (m1 ):shouldBeCalled ():
32
- when (function () m2 () end )
41
+ shouldFailWith ( ' unexpected function "f2" called ' , function ()
42
+ mock (f1 ):shouldBeCalled ():
43
+ when (function () f2 () end )
33
44
end )
34
45
end )
35
46
36
47
it (' should alert you when a function is called unexpectedly' , function ()
37
- local m = mock :mockFunction ()
48
+ local f = mock :mockFunction (' f ' )
38
49
39
- shouldFail ( function ()
40
- m ()
50
+ shouldFailWith ( ' unexpected function "f" called ' , function ()
51
+ f ()
41
52
end )
42
53
end )
43
54
44
55
it (' should allow you to verify that a function has been called with the correct arguments' , function ()
45
- local m = mock :mockFunction ()
56
+ local f = mock :mockFunction (' f ' )
46
57
47
- mock (m ):shouldBeCalledWith (1 , ' 2' ):
48
- when (function () m (1 , ' 2' ) end )
58
+ mock (f ):shouldBeCalledWith (1 , ' 2' ):
59
+ when (function () f (1 , ' 2' ) end )
49
60
end )
50
61
51
62
it (' should alert you when a function has been called with incorrect arguments' , function ()
52
- local m = mock :mockFunction ()
63
+ local f = mock :mockFunction (' f ' )
53
64
54
65
shouldFail (function ()
55
- mock (m ):shouldBeCalledWith (1 , ' 2' ):
56
- when (function () m (1 , ' 3' ) end )
66
+ mock (f ):shouldBeCalledWith (1 , ' 2' ):
67
+ when (function () f (1 , ' 3' ) end )
57
68
end )
58
69
end )
59
70
60
71
it (' should allow you to specify the return value of a mocked function' , function ()
61
- local m = mock :mockFunction ()
72
+ local f = mock :mockFunction (' f ' )
62
73
63
- mock (m ):shouldBeCalled ():andWillReturn (4 ):
74
+ mock (f ):shouldBeCalled ():andWillReturn (4 ):
64
75
when (function ()
65
- assert .is .equal (m (), 4 )
76
+ assert .is .equal (f (), 4 )
66
77
end )
67
78
end )
68
79
69
80
it (' should allow you to specify multiple return values for a mocked function' , function ()
70
- local m = mock :mockFunction ()
81
+ local f = mock :mockFunction (' f ' )
71
82
72
- mock (m ):shouldBeCalled ():andWillReturn (1 , 2 ):
83
+ mock (f ):shouldBeCalled ():andWillReturn (1 , 2 ):
73
84
when (function ()
74
- r1 , r2 = m ()
85
+ r1 , r2 = f ()
75
86
assert .is .equal (r1 , 1 )
76
87
assert .is .equal (r2 , 2 )
77
88
end )
78
89
end )
79
90
80
91
it (' should allow you to check that a function has been called multiple times' , function ()
81
- local m = mock :mockFunction ()
92
+ local f = mock :mockFunction (' f ' )
82
93
83
- mock (m ):shouldBeCalled ():
84
- andAlso (mock (m ):shouldBeCalledWith (1 , 2 , 3 )):
94
+ mock (f ):shouldBeCalled ():
95
+ andAlso (mock (f ):shouldBeCalledWith (1 , 2 , 3 )):
85
96
when (function ()
86
- m ()
87
- m (1 , 2 , 3 )
97
+ f ()
98
+ f (1 , 2 , 3 )
88
99
end )
89
100
end )
90
101
91
102
it (' should allow you to check that multiple functions are called' , function ()
92
- local m1 = mock :mockFunction ()
93
- local m2 = mock :mockFunction ()
103
+ local f1 = mock :mockFunction (' f1 ' )
104
+ local f2 = mock :mockFunction (' f2 ' )
94
105
95
- mock (m1 ):shouldBeCalled ():
96
- andAlso (mock (m2 ):shouldBeCalledWith (1 , 2 , 3 )):
106
+ mock (f1 ):shouldBeCalled ():
107
+ andAlso (mock (f2 ):shouldBeCalledWith (1 , 2 , 3 )):
97
108
when (function ()
98
- m1 ()
99
- m2 (1 , 2 , 3 )
109
+ f1 ()
110
+ f2 (1 , 2 , 3 )
100
111
end )
101
112
end )
102
113
103
114
it (' should allow you to mix and match call types' , function ()
104
- local m1 = mock :mockFunction ()
105
- local m2 = mock :mockFunction ()
115
+ local f1 = mock :mockFunction (' f1 ' )
116
+ local f2 = mock :mockFunction (' f2 ' )
106
117
107
- mock (m1 ):shouldBeCalled ():
108
- andAlso (mock (m2 ):shouldBeCalledWith (1 , 2 , 3 )):
109
- andThen (mock (m2 ):shouldBeCalledWith (1 ):andWillReturn (4 )):
118
+ mock (f1 ):shouldBeCalled ():
119
+ andAlso (mock (f2 ):shouldBeCalledWith (1 , 2 , 3 )):
120
+ andThen (mock (f2 ):shouldBeCalledWith (1 ):andWillReturn (4 )):
110
121
when (function ()
111
- m1 ()
112
- m2 (1 , 2 , 3 )
113
- assert .is .equal (m2 (1 ), 4 )
122
+ f1 ()
123
+ f2 (1 , 2 , 3 )
124
+ assert .is .equal (f2 (1 ), 4 )
114
125
end )
115
126
end )
116
127
117
128
it (' should allow functions to be used to improve readability' , function ()
118
- local m1 = mock :mockFunction ()
119
- local m2 = mock :mockFunction ()
129
+ local f1 = mock :mockFunction (' f1 ' )
130
+ local f2 = mock :mockFunction (' f1 ' )
120
131
121
132
function somethingShouldHappen ()
122
- return mock (m1 ):shouldBeCalled ()
133
+ return mock (f1 ):shouldBeCalled ()
123
134
end
124
135
125
136
function anotherThingShouldHappen ()
126
- return mock (m2 ):shouldBeCalledWith (1 , 2 , 3 )
137
+ return mock (f2 ):shouldBeCalledWith (1 , 2 , 3 )
127
138
end
128
139
129
140
function theCodeUnderTestRuns ()
130
- m1 ()
131
- m2 (1 , 2 , 3 )
141
+ f1 ()
142
+ f2 (1 , 2 , 3 )
132
143
end
133
144
134
145
somethingShouldHappen ():
@@ -142,7 +153,7 @@ describe('The mock library', function()
142
153
bar = function () end
143
154
}
144
155
145
- mockedTable = mock :mockTable (someTable )
156
+ mockedTable = mock :mockTable (someTable , ' someTable ' )
146
157
147
158
mock (mockedTable .foo ):shouldBeCalledWith (1 ):andWillReturn (2 ):
148
159
andAlso (mock (mockedTable .bar ):shouldBeCalled ()):
@@ -199,7 +210,7 @@ describe('The mock library', function()
199
210
end )
200
211
201
212
it (' should let you expect a function to be called multiple times' , function ()
202
- local f = mock :mockFunction ()
213
+ local f = mock :mockFunction (' f ' )
203
214
204
215
mock (f ):shouldBeCalledWith (2 ):andWillReturn (1 ):multipleTimes (3 ):
205
216
when (function ()
@@ -223,7 +234,7 @@ describe('The mock library', function()
223
234
224
235
it (' should fail if a function is called too many times' , function ()
225
236
shouldFail (function ()
226
- local f = mock :mockFunction ()
237
+ local f = mock :mockFunction (' f ' )
227
238
228
239
mock (f ):shouldBeCalledWith (2 ):andWillReturn (1 ):multipleTimes (2 ):
229
240
when (function ()
@@ -236,7 +247,5 @@ describe('The mock library', function()
236
247
237
248
-- ordering
238
249
239
- -- multiple times
240
-
241
250
-- allowed vs. not allowed functions on the expectation (ie: state machine for expectation)
242
251
end )
0 commit comments