Skip to content

Commit 340b072

Browse files
committed
Allow other calls to be ignored. Closes #4.
1 parent d298799 commit 340b072

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ f:should_be_called_with(1):
150150
end)
151151
```
152152

153+
## Ignoring Other calls
154+
155+
```javascript
156+
local mach = require 'mach'
157+
158+
local f = mach.mock_function('f')
159+
160+
f:should_be_called().and_other_calls_should_be_ignored():when(function()
161+
f()
162+
f(1)
163+
end)
164+
```
165+
153166
## Flexible Syntax
154167

155168
```lua

spec/mach_spec.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,11 @@ describe('The mach library', function()
439439
mach.mock_method()(1, 2, 3)
440440
end)
441441
end)
442+
443+
it('should allow additional mocked calls to be ignored', function()
444+
f1:should_be_called():and_other_calls_should_be_ignored():when(function()
445+
f1()
446+
f2()
447+
end)
448+
end)
442449
end)

src/mach/Expectation.lua

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ function expectation:when(thunk)
4343
end
4444

4545
local function called(m, name, args)
46-
assert(#self._calls > 0, 'unexpected call')
47-
4846
local valid_function_found = false
4947

5048
for i, call in ipairs(self._calls) do
@@ -69,10 +67,12 @@ function expectation:when(thunk)
6967
if call:has_fixed_order() then break end
7068
end
7169

72-
if not valid_function_found then
73-
unexpected_call_error(name, args, 2)
74-
else
75-
unexpected_args_error(name, args, 2)
70+
if not self._ignore_other_calls then
71+
if not valid_function_found then
72+
unexpected_call_error(name, args, 2)
73+
else
74+
unexpected_args_error(name, args, 2)
75+
end
7676
end
7777
end
7878

@@ -155,4 +155,9 @@ function expectation:multiple_times(times)
155155
return self
156156
end
157157

158+
function expectation:and_other_calls_should_be_ignored()
159+
self._ignore_other_calls = true
160+
return self
161+
end
162+
158163
return create

0 commit comments

Comments
 (0)