@@ -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
278279Use ` 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
282293sut .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
300307sut .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
319323sut .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
327349Set up indexers with argument matchers. Supports initialization, returns/throws sequences, and callbacks.
0 commit comments