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