@@ -18,6 +18,7 @@ See the full [reference documentation](https://hexdocs.pm/mock/Mock.html).
18
18
* [*setup_with_mocks* - Configure all tests to have the same mocks](#setup_with_mocks---Configure-all-tests-to-have-the-same-mocks)
19
19
* [Mocking input dependent output](#Mocking-input-dependent-output)
20
20
* [Mocking functions with different arities](#Mocking-functions-with-different-arities)
21
+ * [Mocking repeated calls to the same function with different results](#Mocking-repeated-calls-to-the-same-function)
21
22
* [*passthrough* - partial mocking of a module](#passthrough---partial-mocking-of-a-module)
22
23
* [Assert called - assert a specific function was called](#Assert-called---assert-a-specific-function-was-called)
23
24
* [Assert called - specific value](#Assert-called---specific-value)
@@ -204,7 +205,7 @@ defmodule MyTest do
204
205
assert Map .get (%{}, " http://example.tech" ) == " <html>example.tech says hi</html>"
205
206
end
206
207
end
207
-
208
+
208
209
def conditionally_mocked (url) do
209
210
cond do
210
211
String .contains?(url, " .xyz" ) - > " <html>Hello from example.xyz</html>"
237
238
238
239
````
239
240
241
+ ## Mock repeated calls
242
+
243
+ You can mock repeated calls to the same function _ and_ arguments to return
244
+ different results in a series using the ` in_series ` call with static values.
245
+ This does not currently support _ functions_ .
246
+
247
+ ** Caution** : This is only useful in rare instances where the underlying business
248
+ logic is likely to be stateful. If you can avoid it by using different function
249
+ arguments, or refactor the function to be stateful, consider that approach first.
250
+
251
+ ```` elixir
252
+ defmodule MyTest do
253
+ use ExUnit .Case , async: false
254
+
255
+ import Mock
256
+
257
+ test " mock repeated calls with in_series" do
258
+ with_mock String ,
259
+ [slice: [in_series ([" test" , 1 .. 3 ], [" string1" , " string2" , " string3" ])]]
260
+ do
261
+ assert String .slice (" test" , 1 .. 3 ) == " string1"
262
+ assert String .slice (" test" , 1 .. 3 ) == " string2"
263
+ assert String .slice (" test" , 1 .. 3 ) == " string3"
264
+ end
265
+ end
266
+ end
267
+
268
+ ````
269
+
240
270
## * passthrough* - partial mocking of a module
241
271
242
272
By default, only the functions being mocked can be accessed from within the test.
0 commit comments