File tree Expand file tree Collapse file tree 3 files changed +53
-1
lines changed Expand file tree Collapse file tree 3 files changed +53
-1
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,30 @@ describe('The mach library', function()
81
81
end )
82
82
end )
83
83
84
+ it (' should allow you to specify errors to be raised when a mocked function is called' , function ()
85
+ local f = mach .mock_function (' f' )
86
+
87
+ f :should_be_called ():
88
+ and_will_raise_error (' some error message' ):
89
+ when (function ()
90
+ should_fail_with (' some error message' , function ()
91
+ f ()
92
+ end )
93
+ end )
94
+ end )
95
+
96
+ it (' should allow calls to be completed after a call that raises an error' , function ()
97
+ local f = mach .mock_function (' f' )
98
+
99
+ f :should_be_called ():
100
+ and_will_raise_error (' some error message' ):
101
+ and_then (f :should_be_called ():and_will_return (4 )):
102
+ when (function ()
103
+ pcall (function () f () end )
104
+ assert .is .equal (f (), 4 )
105
+ end )
106
+ end )
107
+
84
108
it (' should allow you to check that a function has been called multiple times' , function ()
85
109
local f = mach .mock_function (' f' )
86
110
Original file line number Diff line number Diff line change @@ -27,6 +27,16 @@ function expectation:and_will_return(...)
27
27
return self
28
28
end
29
29
30
+ function expectation :and_will_raise_error (...)
31
+ -- if not self._call_specified then
32
+ -- error('cannot set return value for an unspecified call', 2)
33
+ -- end
34
+
35
+ self ._calls [# self ._calls ]:set_error (... )
36
+
37
+ return self
38
+ end
39
+
30
40
function expectation :when (thunk )
31
41
if not self ._call_specified then
32
42
error (' incomplete expectation' , 2 )
@@ -46,7 +56,13 @@ function expectation:when(thunk)
46
56
self ._calls [i - 1 ]:fix_order ()
47
57
end
48
58
49
- return table.remove (self ._calls , i ):get_return_values ()
59
+ table.remove (self ._calls , i )
60
+
61
+ if call :has_error () then
62
+ error (call :get_error ())
63
+ end
64
+
65
+ return call :get_return_values ()
50
66
end
51
67
end
52
68
Original file line number Diff line number Diff line change @@ -37,6 +37,18 @@ function expected_call:get_return_values(...)
37
37
return table.unpack (self ._return )
38
38
end
39
39
40
+ function expected_call :set_error (...)
41
+ self ._error = table.pack (... )
42
+ end
43
+
44
+ function expected_call :get_error (...)
45
+ return table.unpack (self ._error )
46
+ end
47
+
48
+ function expected_call :has_error ()
49
+ return self ._error ~= nil
50
+ end
51
+
40
52
function expected_call :fix_order ()
41
53
self ._ordered = true
42
54
end
You can’t perform that action at this time.
0 commit comments