Skip to content

Repository files navigation

mach.lua

Simple mocking framework for Lua inspired by CppUMock and designed for readability.

Mocking a Function

localmach=require'mach'localf=mach.mock_function('f')
f.should_be_called().when(function() f() end)

Mocking a Method

localmach=require'mach'localo= {}
o.m=mach.mock_method('m')
o.m.should_be_called().when(function() o:m() end)

Mocking a Table

localmach=require'mach'localsome_table= {
foo=function() end,
bar=function() end
}
mocked_table=mach.mock_table(some_table, 'some_table')
mocked_table.foo.should_be_called().when(function()
mocked_table.foo()
end)

Mocking an Object

localmach=require'mach'localsome_object= {}
functionsome_object:foo() endfunctionsome_object:bar() endmocked_object=mach.mock_object(some_object, 'some_object')
mocked_object.foo.should_be_called().when(function()
mocked_object:foo()
end)

Ignoring Arguments

localmach=require'mach'localf=mach.mock_function('f')
f.should_be_called_with_any_arguments().when(function() f('any', 'args', 'are', 'fine') end)

Returning Values

localmach=require'mach'localf=mach.mock_function('f')
f.should_be_called().and_will_return(1, 4).when(function()
localx, y=f()
end)
f.should_be_called().with_result(1, 4).when(function()
localx, y=f()
end)

Raising Errors

localmach=require'mach'localf=mach.mock_function('f')
f.should_be_called().and_will_raise_error('some error').when(function()
f()
end)

Multiple Calls to the Same Function

localmach=require'mach'localf=mach.mock_function('f')
f.should_be_called().multiple_times(2).when(function()
f()
f()
end)

Multiple Expectations

localmach=require'mach'localf1=mach.mock_function('f1')
localf2=mach.mock_function('f2')
f1.should_be_called()
.and_also(f2.should_be_called())
.when(function()
f1()
f2()
end)

Optional Expectations

localmach=require'mach'localf=mach.mock_function('f')
f.may_be_called().when(function() end)

Optional Ordering

localmach=require'mach'localf=mach.mock_function('f')
-- Use and_then when order is importantf.should_be_called_with(1)
.and_then(f.should_be_called_with(2))
.when(function()
f(2) -- Error, out of order callf(1)
end)
-- Use and_also when order is unimportantf.should_be_called_with(1)
.and_also(f.should_be_called_with(2))
.when(function()
f(2) -- No error, order is not fixed when 'and_also' is usedf(1)
end)

Mixed Ordering

localmach=require'mach'localf=mach.mock_function('f')
f.should_be_called_with(1)
.and_also(f.should_be_called_with(2))
.and_then(f.should_be_called_with(3))
.and_also(f.should_be_called_with(4))
.when(function()
f(2)
f(1)
f(4)
f(3)
end)

Matching Arguments Using Deep Compare

localmach=require'mach'localf=mach.mockFunction();
f.should_be_called_with(mach.match({ 1, 2, 3 }))
.when(function()
f({ 1, 2, 3 })
end)

Matching Arguments Using a Custom Matcher

localmach=require'mach'localcustom_matcher=function(a, b)
returna[1] ==b[1]
endlocalf=mach.mockFunction();
f.should_be_called_with(mach.match({ 1, 2, 3 }, custom_matcher))
.when(function()
f({ 1, 4, 9 })
end)

Matching Any Single Argument

localmach=require'mach'localf=mach.mockFunction();
f.should_be_called_with(mach.any, 42)
.when(function()
f({ 'whatever' }, 42)
end)

Ignoring Other Calls

localmach=require'mach'localf=mach.mock_function('f')
f.should_be_called().and_other_calls_should_be_ignored().when(function()
f()
f(1)
end)
localmach=require'mach'localf=mach.mock_function('f')
f.should_be_called().with_other_calls_ignored().when(function()
f()
f(1)
end)

Ignoring All Calls

localmach=require'mach'localf=mach.mock_function('f')
mach.ignore_mocked_calls_when(function()
f()
f(1, 2, 3)
end)

Flexible Syntax

localmach=require'mach'localm1=mach.mock_function('m1')
localm2=mach.mock_function('m2')
functionsomething_should_happen()
returnm1.should_be_called()
endfunctionanother_thing_should_happen()
returnm2.should_be_called_with(1, 2, 3)
endfunctionthe_code_under_test_runs()
m1()
m2(1, 2, 3)
end-- Actual test:something_should_happen()
.and_also(another_thing_should_happen())
.when(the_code_under_test_runs)

Handy Error messages

localmach=require'mach'localf1=mach.mock_function('f1')
localf2=mach.mock_function('f2')
localf2=mach.mock_function('f3')
f1.should_be_called_with(1)
.and_also(f2.should_be_called_with(2))
.when(function()
f1(1)
f3(3)
end)
Unexpected function call f3(3)
Completed calls:
f1(1)
Incomplete calls:
f2(2)

About

Simple mocking framework for Lua inspired by CppUMock and designed for readability

Resources

Stars

17 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages