Skip to content

Commit fd3a517

Browse files
committed
Better error message when an invalid expectation is used
1 parent c3bddb8 commit fd3a517

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

spec/mach_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,4 +750,10 @@ describe('The mach library', function()
750750
end)
751751
end)
752752
end)
753+
754+
it('should give a helpful error message if a non-existent expectation is used', function()
755+
should_fail_with("attempt to call a nil value (field 'should_be_call')", function()
756+
f:should_be_call()
757+
end)
758+
end)
753759
end)

src/mach.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ end
3333
local function create_expectation(_, method)
3434
return function(self, ...)
3535
local expectation = Expectation(handle_mock_calls, self)
36+
if not expectation[method] then
37+
error("attempt to call a nil value (field '" .. method .. "')", 2)
38+
end
3639
return expectation[method](expectation, ...)
3740
end
3841
end

0 commit comments

Comments
 (0)