@@ -6,99 +6,99 @@ Simple mocking framework for Lua inspired by CppUMock and designed for readabili
6
6
## Mocking a Function
7
7
8
8
``` lua
9
- mock = require ' mach'
9
+ mach = require ' mach'
10
10
11
- local f = mock .mockFunction ()
11
+ local f = mach .mockFunction ()
12
12
13
- mock (f ):shouldBeCalled ():
13
+ mach (f ):shouldBeCalled ():
14
14
when (function () f () end )
15
15
```
16
16
17
17
## Mocking a Method
18
18
19
19
``` lua
20
- mock = require ' mach'
20
+ mach = require ' mach'
21
21
22
22
local o = {}
23
- o .m = mock .mockMethod ()
23
+ o .m = mach .mockMethod ()
24
24
25
- mock (m ):shouldBeCalled ():
25
+ mach (m ):shouldBeCalled ():
26
26
when (function () o :m () end )
27
27
```
28
28
29
29
## Mocking a Table
30
30
31
31
``` lua
32
- mock = require ' mach'
32
+ mach = require ' mach'
33
33
34
34
local someTable = {
35
35
foo = function () end ,
36
36
bar = function () end
37
37
}
38
38
39
- mockedTable = mock .mockTable (someTable )
39
+ mockedTable = mach .mockTable (someTable )
40
40
41
- mock (mockedTable .foo ):shouldBeCalled ():
41
+ mach (mockedTable .foo ):shouldBeCalled ():
42
42
when (function () mockedTable .foo () end )
43
43
```
44
44
45
45
## Mocking an Object
46
46
47
47
``` lua
48
- mock = require ' mach'
48
+ mach = require ' mach'
49
49
50
50
local someObject = {}
51
51
function someObject :foo () end
52
52
function someObject :bar () end
53
53
54
- mockedObject = mock .mockObject (someObject )
54
+ mockedObject = mach .mockObject (someObject )
55
55
56
- mock (mockedObject .foo ):shouldBeCalled ():
56
+ mach (mockedObject .foo ):shouldBeCalled ():
57
57
when (function () mockedObject :foo () end )
58
58
```
59
59
60
60
## Multiple Expectations
61
61
62
62
``` lua
63
- mock = require ' mach'
63
+ mach = require ' mach'
64
64
65
- local f1 = mock .mockFunction ()
66
- local f2 = mock .mockFunction ()
65
+ local f1 = mach .mockFunction ()
66
+ local f2 = mach .mockFunction ()
67
67
68
- mock (f1 ):shouldBeCalled ():
69
- andAlso (mock (f2 ):shouldBeCalled ()):
68
+ mach (f1 ):shouldBeCalled ():
69
+ andAlso (mach (f2 ):shouldBeCalled ()):
70
70
when (function () f1 (); f2 () end )
71
71
```
72
72
73
73
## Optional Expectations
74
74
75
75
``` lua
76
- mock = require ' mach'
76
+ mach = require ' mach'
77
77
78
- local f = mock .mockFunction ()
78
+ local f = mach .mockFunction ()
79
79
80
- mock (f ):mayBeCalled ():
80
+ mach (f ):mayBeCalled ():
81
81
when (function () end )
82
82
```
83
83
84
84
## Optional Ordering
85
85
86
86
``` lua
87
- mock = require ' mach'
87
+ mach = require ' mach'
88
88
89
- local f = mock .mockFunction ()
89
+ local f = mach .mockFunction ()
90
90
91
91
-- Use andAlso when order is important
92
- mock (f ):shouldBeCalledWith (1 ):
93
- andThen (mock (f ):shouldBeCalledWith (2 )):
92
+ mach (f ):shouldBeCalledWith (1 ):
93
+ andThen (mach (f ):shouldBeCalledWith (2 )):
94
94
when (function ()
95
95
f (2 ) -- Error, out of order call
96
96
f (1 )
97
97
end )
98
98
99
99
-- Use andAlso when order is unimportant
100
- mock (f ):shouldBeCalledWith (1 ):
101
- andAlso (mock (f ):shouldBeCalledWith (2 )):
100
+ mach (f ):shouldBeCalledWith (1 ):
101
+ andAlso (mach (f ):shouldBeCalledWith (2 )):
102
102
when (function ()
103
103
f (2 ) -- No error, order is not fixed when 'andAlso' is used
104
104
f (1 )
@@ -108,14 +108,14 @@ end)
108
108
## Mixed Ordering
109
109
110
110
``` lua
111
- mock = require ' mach'
111
+ mach = require ' mach'
112
112
113
- local f = mock .mockFunction ()
113
+ local f = mach .mockFunction ()
114
114
115
- mock (f ):shouldBeCalledWith (1 ):
116
- andAlso (mock (f ):shouldBeCalledWith (2 )):
117
- andThen (mock (f ):shouldBeCalledWith (3 )):
118
- andAlso (mock (f ):shouldBeCalledWith (4 )):
115
+ mach (f ):shouldBeCalledWith (1 ):
116
+ andAlso (mach (f ):shouldBeCalledWith (2 )):
117
+ andThen (mach (f ):shouldBeCalledWith (3 )):
118
+ andAlso (mach (f ):shouldBeCalledWith (4 )):
119
119
when (function ()
120
120
f (2 )
121
121
f (1 )
@@ -127,17 +127,17 @@ end)
127
127
## Flexible Syntax
128
128
129
129
``` lua
130
- mock = require ' mach'
130
+ mach = require ' mach'
131
131
132
- local m1 = mock .mockFunction ()
133
- local m2 = mock .mockFunction ()
132
+ local m1 = mach .mockFunction ()
133
+ local m2 = mach .mockFunction ()
134
134
135
135
function somethingShouldHappen ()
136
- return mock (m1 ):shouldBeCalled ()
136
+ return mach (m1 ):shouldBeCalled ()
137
137
end
138
138
139
139
function anotherThingShouldHappen ()
140
- return mock (m2 ):shouldBeCalledWith (1 , 2 , 3 )
140
+ return mach (m2 ):shouldBeCalledWith (1 , 2 , 3 )
141
141
end
142
142
143
143
function theCodeUnderTestRuns ()
0 commit comments