File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -159,12 +159,18 @@ function Mock:mockMethod(name)
159
159
return m
160
160
end
161
161
162
+ function IsCallable (x )
163
+ local isFunction = type (x ) == ' function'
164
+ local hasCallMetamethod = type ((debug.getmetatable (x ) or {}).__call ) == ' function'
165
+ return isFunction or hasCallMetamethod
166
+ end
167
+
162
168
function Mock :mockTable (t , name )
163
169
name = name or ' <anonymous>'
164
170
local mocked = {}
165
171
166
172
for k , v in pairs (t ) do
167
- if type (v ) == ' function ' then
173
+ if IsCallable (v ) then
168
174
mocked [k ] = self :mockFunction (name .. ' .' .. tostring (k ))
169
175
end
170
176
end
@@ -177,7 +183,7 @@ function Mock:mockObject(o, name)
177
183
local mocked = {}
178
184
179
185
for k , v in pairs (o ) do
180
- if type (v ) == ' function ' then
186
+ if IsCallable (v ) then
181
187
mocked [k ] = self :mockMethod (name .. ' :' .. tostring (k ))
182
188
end
183
189
end
Original file line number Diff line number Diff line change @@ -194,6 +194,32 @@ describe('The mock library', function()
194
194
end )
195
195
end )
196
196
197
+ it (' should allow mocking of any callable in an object, not just functions' , function ()
198
+ local someTable = {
199
+ foo = {}
200
+ }
201
+
202
+ setmetatable (someTable .foo , {__call = function () end })
203
+
204
+ local mockedTable = mock :mockTable (someTable )
205
+
206
+ mock (mockedTable .foo ):shouldBeCalled ():
207
+ when (function () mockedTable .foo () end )
208
+ end )
209
+
210
+ it (' should allow mocking of any callable in a table, not just functions' , function ()
211
+ local someObject = {
212
+ foo = {}
213
+ }
214
+
215
+ setmetatable (someObject .foo , {__call = function () end })
216
+
217
+ local mockedObject = mock :mockObject (someObject )
218
+
219
+ mock (mockedObject .foo ):shouldBeCalled ():
220
+ when (function () mockedObject :foo () end )
221
+ end )
222
+
197
223
it (' should fail when a method is incorrectly used as a function' , function ()
198
224
shouldFail (function ()
199
225
local someObject = {}
You can’t perform that action at this time.
0 commit comments