Skip to content

Commit 1ad75c7

Browse files
committed
Merge branch 'master' of github.com:ryanplusplus/mock.lua
2 parents 39e642f + 85c4c9b commit 1ad75c7

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
mock.lua
2+
========
3+
4+
Simple mocking framework for Lua based on CppUMock designed for readability.
5+
6+
## Mocking a Function
7+
8+
mock = require 'Mock'
9+
10+
local f = mock:mockFunction()
11+
12+
mock(f):shouldBeCalled():
13+
when(function() f() end)
14+
15+
## Mocking a Method
16+
17+
mock = require 'Mock'
18+
19+
local o = {}
20+
o.m = mock:mockMethod()
21+
22+
mock(m):shouldBeCalled():
23+
when(function() o:m() end)
24+
25+
## Mocking a Table
26+
27+
mock = require 'Mock'
28+
29+
local someTable = {
30+
foo = function() end,
31+
bar = function() end
32+
}
33+
34+
mockedTable = mock:mockTable(someTable)
35+
36+
mock(mockedTable.foo):shouldBeCalled():
37+
when(function() mockedTable.foo() end)
38+
39+
## Mocking an Object
40+
41+
mock = require 'Mock'
42+
43+
local someObject = {}
44+
function someObject:foo() end
45+
function someObject:bar() end
46+
47+
mockedObject = mock:mockObject(someObject)
48+
49+
mock(mockedObject.foo):shouldBeCalled():
50+
when(function() mockedObject:foo() end)
51+
52+
## Multiple Expectations
53+
54+
mock = require 'Mock'
55+
56+
local f1 = mock:mockFunction()
57+
local f2 = mock:mockFunction()
58+
59+
mock(f1):shouldBeCalled():
60+
andAlso(mock(f2):shouldBeCalled()):
61+
when(function() f1(); f2() end)
62+
63+
## Extra Credit For Readability
64+
65+
mock = require 'Mock'
66+
67+
local m1 = mock:mockFunction()
68+
local m2 = mock:mockFunction()
69+
70+
function somethingShouldHappen()
71+
return mock(m1):shouldBeCalled()
72+
end
73+
74+
function anotherThingShouldHappen()
75+
return mock(m2):shouldBeCalledWith(1, 2, 3)
76+
end
77+
78+
function theCodeUnderTestRuns()
79+
m1()
80+
m2(1, 2, 3)
81+
end
82+
83+
-- Actual test:
84+
somethingShouldHappen():
85+
andAlso(anotherThingShouldHappen()):
86+
when(theCodeUnderTestRuns)

0 commit comments

Comments
 (0)