Skip to content

Commit 9b026a0

Browse files
committed
Add support for :multipleTimes
1 parent 1ad75c7 commit 9b026a0

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

mock.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ function MockExpectation:shouldBeCalled()
124124
return self:shouldBeCalledWith()
125125
end
126126

127+
function MockExpectation:multipleTimes(times)
128+
for i = 1, times - 1 do
129+
table.insert(self._calls, self._calls[#self._calls])
130+
end
131+
132+
return self
133+
end
134+
127135

128136

129137
function Mock:mockFunction()

spec/mock_spec.lua

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ describe('The mock library', function()
126126
return mock(m2):shouldBeCalledWith(1, 2, 3)
127127
end
128128

129-
function codeUnderTestRuns()
129+
function theCodeUnderTestRuns()
130130
m1()
131131
m2(1, 2, 3)
132132
end
133133

134134
somethingShouldHappen():
135135
andAlso(anotherThingShouldHappen()):
136-
when(codeUnderTestRuns)
136+
when(theCodeUnderTestRuns)
137137
end)
138138

139139
it('should allow a table of functions to be mocked', function()
@@ -173,7 +173,7 @@ describe('The mock library', function()
173173
function someObject:foo() end
174174
function someObject:bar() end
175175

176-
mockedObject = mock:mockObject(someObject)
176+
local mockedObject = mock:mockObject(someObject)
177177

178178
mock(mockedObject.foo):shouldBeCalledWith(1):andWillReturn(2):
179179
andAlso(mock(mockedObject.bar):shouldBeCalled()):
@@ -189,7 +189,7 @@ describe('The mock library', function()
189189

190190
function someObject:foo() end
191191

192-
mockedObject = mock:mockObject(someObject)
192+
local mockedObject = mock:mockObject(someObject)
193193

194194
mock(mockedObject.foo):shouldBeCalledWith(1):andWillReturn(2):
195195
when(function()
@@ -198,7 +198,45 @@ describe('The mock library', function()
198198
end)
199199
end)
200200

201+
it('should let you expect a function to be called multiple times', function()
202+
local f = mock:mockFunction()
203+
204+
mock(f):shouldBeCalledWith(2):andWillReturn(1):multipleTimes(3):
205+
when(function()
206+
assert(f(2) == 1)
207+
assert(f(2) == 1)
208+
assert(f(2) == 1)
209+
end)
210+
end)
211+
212+
it('should fail if a function is not called enough times', function()
213+
shouldFail(function()
214+
local f = mock:mockFunction()
215+
216+
mock(f):shouldBeCalledWith(2):andWillReturn(1):multipleTimes(3):
217+
when(function()
218+
assert(f(2) == 1)
219+
assert(f(2) == 1)
220+
end)
221+
end)
222+
end)
223+
224+
it('should fail if a function is called too many times', function()
225+
shouldFail(function()
226+
local f = mock:mockFunction()
227+
228+
mock(f):shouldBeCalledWith(2):andWillReturn(1):multipleTimes(2):
229+
when(function()
230+
assert(f(2) == 1)
231+
assert(f(2) == 1)
232+
assert(f(2) == 1)
233+
end)
234+
end)
235+
end)
236+
201237
-- ordering
202238

239+
-- multiple times
240+
203241
-- allowed vs. not allowed functions on the expectation (ie: state machine for expectation)
204242
end)

0 commit comments

Comments
 (0)