Skip to content

Commit 0bfa37a

Browse files
committed
docs: improve method setup documentation
1 parent 193ebe8 commit 0bfa37a

File tree

3 files changed

+105
-60
lines changed

3 files changed

+105
-60
lines changed

Docs/pages/setup/01-properties.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ sut.SetupMock.Property.TotalDispensed.OnGet
7474

7575
**Notes:**
7676

77+
- Use `.SkippingBaseClass(…)` to override the base class behavior for a specific property (only for class mocks).
7778
- All callbacks support more advanced features like conditional execution, frequency control, parallel execution, and
7879
access to the invocation counter.
7980
See [Advanced callback features](https://awexpect.com/docs/mockolate/advanced-features/advanced-callback-features)

Docs/pages/setup/02-methods.md

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
Use `mock.SetupMock.Method.MethodName(…)` to set up methods. You can specify argument matchers for each parameter.
44

5+
## Returns / Throws
6+
7+
Use `.Returns(…)` to specify the value to return. You can provide a direct value, a callback, or a callback with
8+
parameters.
9+
Use `.Throws(…)` to specify an exception to throw. Supports direct exceptions, exception factories, or factories with
10+
parameters.
11+
12+
You can call `.Returns(…)` and `.Throws(…)` multiple times to define a sequence of return values or exceptions (cycled
13+
on each call).
14+
515
```csharp
616
// Setup Dispense to decrease stock and raise event
717
sut.SetupMock.Method.Dispense(It.Is("Dark"), It.IsAny<int>())
@@ -17,32 +27,44 @@ sut.SetupMock.Method.Dispense(It.Is("Dark"), It.IsAny<int>())
1727
return false;
1828
});
1929

20-
// Setup method with callback
21-
sut.SetupMock.Method.Dispense(It.Is("White"), It.IsAny<int>())
22-
.Do((type, amount) => Console.WriteLine($"Dispensed {amount} {type} chocolate."));
23-
2430
// Setup method to throw
2531
sut.SetupMock.Method.Dispense(It.Is("Green"), It.IsAny<int>())
2632
.Throws<InvalidChocolateException>();
27-
```
2833

29-
- Use `.Do(…)` to run code when the method is called. Supports parameterless or parameter callbacks.
30-
- Use `.Returns(…)` to specify the value to return. You can provide a direct value, a callback, or a callback with
31-
parameters.
32-
- Use `.Throws(…)` to specify an exception to throw. Supports direct exceptions, exception factories, or factories with
33-
parameters.
34-
- Use `.Returns(…)` and `.Throws(…)` repeatedly to define a sequence of return values or exceptions (cycled on each
35-
call).
36-
- Use `.SkippingBaseClass(…)` to override the base class behavior for a specific method (only for class mocks).
37-
- When you specify overlapping setups, the most recently defined setup takes precedence.
34+
// Sequence of returns and throws
35+
sut.SetupMock.Method.Dispense(It.IsAny<string>(), It.IsAny<int>())
36+
.Returns(true)
37+
.Throws(new Exception("Error"))
38+
.Returns(false);
39+
```
3840

39-
## Async Methods
41+
### Async Methods
4042

41-
For `Task<T>` or `ValueTask<T>` methods, use `.ReturnsAsync(…)` or `ThrowsAsync(…)`:
43+
For async methods returning `Task`/`Task<T>` or `ValueTask`/`ValueTask<T>`, use `.ReturnsAsync(…)` or `ThrowsAsync(…)`
44+
respectively:
4245

4346
```csharp
4447
sut.SetupMock.Method.DispenseAsync(It.IsAny<string>(), It.IsAny<int>())
4548
.ReturnsAsync((_, v) => v) // First execution returns the value of the `int` parameter
4649
.ThrowsAsync(new TimeoutException()) // Second execution throws a TimeoutException
4750
.ReturnsAsync(0).Forever(); // Subsequent executions return 0
4851
```
52+
53+
## Callbacks
54+
55+
Use `.Do(…)` to run code when the method is called. Supports parameterless or parameter callbacks.
56+
57+
```csharp
58+
// Setup method with callback
59+
sut.SetupMock.Method.Dispense(It.Is("White"), It.IsAny<int>())
60+
.Do((type, amount) => Console.WriteLine($"Dispensed {amount} {type} chocolate."));
61+
```
62+
63+
**Notes:**
64+
65+
- Use `.SkippingBaseClass(…)` to override the base class behavior for a specific method (only for class mocks).
66+
- When you specify overlapping setups, the most recently defined setup takes precedence.
67+
- All callbacks and return values support more advanced features like conditional execution, frequency control,
68+
parallel execution, and access to the invocation counter.
69+
See [Advanced callback features](https://awexpect.com/docs/mockolate/advanced-features/advanced-callback-features)
70+
for details.

README.md

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -118,37 +118,37 @@ var classMock = Mock.Create<MyChocolateDispenser>(
118118
**`MockBehavior` options**
119119

120120
- `ThrowWhenNotSetup` (bool):
121-
- If `false` (default), the mock will return a default value (see `DefaultValue`).
122-
- If `true`, the mock will throw an exception when a method or property is called without a setup.
121+
- If `false` (default), the mock will return a default value (see `DefaultValue`).
122+
- If `true`, the mock will throw an exception when a method or property is called without a setup.
123123
- `SkipBaseClass` (bool):
124-
- If `false` (default), the mock will call the base class implementation and use its return values as default
125-
values, if no explicit setup is defined.
126-
- If `true`, the mock will not call any base class implementations.
124+
- If `false` (default), the mock will call the base class implementation and use its return values as default
125+
values, if no explicit setup is defined.
126+
- If `true`, the mock will not call any base class implementations.
127127
- `Initialize<T>(params Action<IMockSetup<T>>[] setups)`:
128-
- Automatically initialize all mocks of type T with the given setups when they are created.
129-
- The callback can optionally receive an additional counter parameter which allows to differentiate between multiple
130-
instances. This is useful when you want to ensure that you can distinguish between different automatically created
131-
instances.
128+
- Automatically initialize all mocks of type T with the given setups when they are created.
129+
- The callback can optionally receive an additional counter parameter which allows to differentiate between multiple
130+
instances. This is useful when you want to ensure that you can distinguish between different automatically created
131+
instances.
132132
- `DefaultValue` (IDefaultValueGenerator):
133-
- Customizes how default values are generated for methods/properties that are not set up.
134-
- The default implementation provides sensible defaults for the most common use cases:
135-
- Empty collections for collection types (e.g., `IEnumerable<T>`, `List<T>`, etc.)
136-
- Empty string for `string`
137-
- Completed tasks for `Task`, `Task<T>`, `ValueTask` and `ValueTask<T>`
138-
- Tuples with recursively defaulted values
139-
- `null` for other reference types
140-
- You can provide custom default values for specific types using `.WithDefaultValueFor<T>()`:
141-
```csharp
142-
var behavior = MockBehavior.Default
143-
.WithDefaultValueFor<string>(() => "default")
144-
.WithDefaultValueFor<int>(() => 42);
145-
var sut = Mock.Create<IChocolateDispenser>(behavior);
146-
```
147-
This is useful when you want mocks to return specific default values for certain types instead of the standard
148-
defaults (e.g., `null`, `0`, empty strings).
133+
- Customizes how default values are generated for methods/properties that are not set up.
134+
- The default implementation provides sensible defaults for the most common use cases:
135+
- Empty collections for collection types (e.g., `IEnumerable<T>`, `List<T>`, etc.)
136+
- Empty string for `string`
137+
- Completed tasks for `Task`, `Task<T>`, `ValueTask` and `ValueTask<T>`
138+
- Tuples with recursively defaulted values
139+
- `null` for other reference types
140+
- You can provide custom default values for specific types using `.WithDefaultValueFor<T>()`:
141+
```csharp
142+
var behavior = MockBehavior.Default
143+
.WithDefaultValueFor<string>(() => "default")
144+
.WithDefaultValueFor<int>(() => 42);
145+
var sut = Mock.Create<IChocolateDispenser>(behavior);
146+
```
147+
This is useful when you want mocks to return specific default values for certain types instead of the standard
148+
defaults (e.g., `null`, `0`, empty strings).
149149
- `.UseConstructorParametersFor<T>(object?[])`:
150-
- Configures constructor parameters to use when creating mocks of type `T`, unless explicit parameters are provided
151-
during mock creation via `BaseClass.WithConstructorParameters(…)`.
150+
- Configures constructor parameters to use when creating mocks of type `T`, unless explicit parameters are provided
151+
during mock creation via `BaseClass.WithConstructorParameters(…)`.
152152

153153
### Using a factory for shared behavior
154154

@@ -269,14 +269,25 @@ sut.SetupMock.Property.TotalDispensed.OnGet
269269

270270
*Notes:*
271271

272-
- All callbacks support more advanced features like conditional execution, frequency control, parallel execution, and
273-
access to the invocation counter.
272+
- Use `.SkippingBaseClass(…)` to override the base class behavior for a specific property (only for class mocks).
273+
- All callbacks and return values support more advanced features like conditional execution, frequency control,
274+
parallel execution, and access to the invocation counter.
274275
See [Advanced callback features](#advanced-callback-features) for details.
275276

276277
### Methods
277278

278279
Use `mock.SetupMock.Method.MethodName(…)` to set up methods. You can specify argument matchers for each parameter.
279280

281+
**Returns / Throws**
282+
283+
Use `.Returns(…)` to specify the value to return. You can provide a direct value, a callback, or a callback with
284+
parameters.
285+
Use `.Throws(…)` to specify an exception to throw. Supports direct exceptions, exception factories, or factories with
286+
parameters.
287+
288+
You can call `.Returns(…)` and `.Throws(…)` multiple times to define a sequence of return values or exceptions (cycled
289+
on each call).
290+
280291
```csharp
281292
// Setup Dispense to decrease stock and raise event
282293
sut.SetupMock.Method.Dispense(It.Is("Dark"), It.IsAny<int>())
@@ -292,28 +303,21 @@ sut.SetupMock.Method.Dispense(It.Is("Dark"), It.IsAny<int>())
292303
return false;
293304
});
294305

295-
// Setup method with callback
296-
sut.SetupMock.Method.Dispense(It.Is("White"), It.IsAny<int>())
297-
.Do((type, amount) => Console.WriteLine($"Dispensed {amount} {type} chocolate."));
298-
299306
// Setup method to throw
300307
sut.SetupMock.Method.Dispense(It.Is("Green"), It.IsAny<int>())
301308
.Throws<InvalidChocolateException>();
302-
```
303309

304-
- Use `.Do(…)` to run code when the method is called. Supports parameterless or parameter callbacks.
305-
- Use `.Returns(…)` to specify the value to return. You can provide a direct value, a callback, or a callback with
306-
parameters.
307-
- Use `.Throws(…)` to specify an exception to throw. Supports direct exceptions, exception factories, or factories with
308-
parameters.
309-
- Use `.Returns(…)` and `.Throws(…)` repeatedly to define a sequence of return values or exceptions (cycled on each
310-
call).
311-
- Use `.SkippingBaseClass(…)` to override the base class behavior for a specific method (only for class mocks).
312-
- When you specify overlapping setups, the most recently defined setup takes precedence.
310+
// Sequence of returns and throws
311+
sut.SetupMock.Method.Dispense(It.IsAny<string>(), It.IsAny<int>())
312+
.Returns(true)
313+
.Throws(new Exception("Error"))
314+
.Returns(false);
315+
```
313316

314317
**Async Methods**
315318

316-
For `Task<T>` or `ValueTask<T>` methods, use `.ReturnsAsync(…)` or `ThrowsAsync(…)`:
319+
For async methods returning `Task`/`Task<T>` or `ValueTask`/`ValueTask<T>`, use `.ReturnsAsync(…)` or `ThrowsAsync(…)`
320+
respectively:
317321

318322
```csharp
319323
sut.SetupMock.Method.DispenseAsync(It.IsAny<string>(), It.IsAny<int>())
@@ -322,6 +326,24 @@ sut.SetupMock.Method.DispenseAsync(It.IsAny<string>(), It.IsAny<int>())
322326
.ReturnsAsync(0).Forever(); // Subsequent executions return 0
323327
```
324328

329+
**Callbacks**
330+
331+
Use `.Do(…)` to run code when the method is called. Supports parameterless or parameter callbacks.
332+
333+
```csharp
334+
// Setup method with callback
335+
sut.SetupMock.Method.Dispense(It.Is("White"), It.IsAny<int>())
336+
.Do((type, amount) => Console.WriteLine($"Dispensed {amount} {type} chocolate."));
337+
```
338+
339+
*Notes:*
340+
341+
- Use `.SkippingBaseClass(…)` to override the base class behavior for a specific method (only for class mocks).
342+
- When you specify overlapping setups, the most recently defined setup takes precedence.
343+
- All callbacks support more advanced features like conditional execution, frequency control, parallel execution, and
344+
access to the invocation counter.
345+
See [Advanced callback features](#advanced-callback-features) for details.
346+
325347
### Indexers
326348

327349
Set up indexers with argument matchers. Supports initialization, returns/throws sequences, and callbacks.

0 commit comments

Comments
 (0)