From 0994da666f6687e5cc3d80fd4630d147c75eee7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 31 Jan 2026 12:50:28 +0100 Subject: [PATCH 1/2] docs: document `ThrowsAsync` --- Docs/pages/01-create-mocks.md | 2 +- Docs/pages/setup/01-properties.md | 6 +++--- Docs/pages/setup/02-methods.md | 8 +++++--- Docs/pages/setup/04-parameter-matching.md | 16 ++++++++-------- Docs/pages/special-types/02-delegates.md | 4 ++-- README.md | 6 ++++-- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Docs/pages/01-create-mocks.md b/Docs/pages/01-create-mocks.md index 9484d29d..135b9870 100644 --- a/Docs/pages/01-create-mocks.md +++ b/Docs/pages/01-create-mocks.md @@ -38,7 +38,7 @@ var classMock = Mock.Create( ); ``` -**`MockBehavior` options** +### `MockBehavior` options - `ThrowWhenNotSetup` (bool): - If `false` (default), the mock will return a default value (see `DefaultValue`). diff --git a/Docs/pages/setup/01-properties.md b/Docs/pages/setup/01-properties.md index e287a0d7..55b5017b 100644 --- a/Docs/pages/setup/01-properties.md +++ b/Docs/pages/setup/01-properties.md @@ -2,7 +2,7 @@ Set up property getters and setters to control or verify property access on your mocks. -**Initialization** +## Initialization You can initialize properties so they work like normal properties (setter changes the value, getter returns the last set value): @@ -11,7 +11,7 @@ value): sut.SetupMock.Property.TotalDispensed.InitializeWith(42); ``` -**Returns / Throws** +## Returns / Throws Alternatively, set up properties with `Returns` and `Throws` (supports sequences): @@ -23,7 +23,7 @@ sut.SetupMock.Property.TotalDispensed .Returns(4); ``` -**Callbacks** +## Callbacks Register callbacks on the setter or getter: diff --git a/Docs/pages/setup/02-methods.md b/Docs/pages/setup/02-methods.md index e023e7a7..37820c42 100644 --- a/Docs/pages/setup/02-methods.md +++ b/Docs/pages/setup/02-methods.md @@ -36,11 +36,13 @@ sut.SetupMock.Method.Dispense(It.Is("Green"), It.IsAny()) - Use `.SkippingBaseClass(…)` to override the base class behavior for a specific method (only for class mocks). - When you specify overlapping setups, the most recently defined setup takes precedence. -**Async Methods** +## Async Methods -For `Task` or `ValueTask` methods, use `.ReturnsAsync(…)`: +For `Task` or `ValueTask` methods, use `.ReturnsAsync(…)` or `ThrowsAsync(…)`: ```csharp sut.SetupMock.Method.DispenseAsync(It.IsAny(), It.IsAny()) - .ReturnsAsync(true); + .ReturnsAsync((_, v) => v) // First execution returns the value of the `int` parameter + .ThrowsAsync(new TimeoutException()) // Second execution throws a TimeoutException; + .ReturnsAsync(0).Forever(); // Subsequent executions return 0 ``` diff --git a/Docs/pages/setup/04-parameter-matching.md b/Docs/pages/setup/04-parameter-matching.md index a7c3b7e2..bdcbca66 100644 --- a/Docs/pages/setup/04-parameter-matching.md +++ b/Docs/pages/setup/04-parameter-matching.md @@ -4,7 +4,7 @@ Mockolate provides flexible parameter matching for method setups and verificatio ## Parameter Matchers -**Basic Matchers** +### Basic Matchers - `It.IsAny()`: Matches any value of type `T`. - `It.Is(value)`: Matches a specific value. @@ -15,11 +15,11 @@ Mockolate provides flexible parameter matching for method setups and verificatio minimum and maximum value. - `It.Satisfies(predicate)`: Matches values based on a predicate. -**String Matching** +### String Matching - `It.Matches(pattern)`: Matches strings using wildcard patterns (`*` and `?`). -**Regular Expressions** +#### Regular Expressions Use `.AsRegex()` to enable regular expression matching for `It.Matches()`: ```csharp @@ -34,7 +34,7 @@ sut.SetupMock.Method.Process(It.Matches("^[A-Z]+$").AsRegex().CaseSensitive()) .Returns(1); ``` -**Ref and Out Parameters** +### Ref and Out Parameters - `It.IsRef(setter)`: Matches any `ref` parameter and sets a new value using the setter function. - `It.IsRef(predicate, setter)`: Matches `ref` parameters that satisfy the predicate and sets a new value. @@ -62,7 +62,7 @@ sut.Increment(ref value); // value == 6 ``` -**Span Parameters (.NET 8+)** +### Span Parameters (.NET 8+) - `It.IsSpan(predicate)`: Matches `Span` parameters that satisfy the predicate. - `It.IsAnySpan()`: Matches any `Span` parameter. @@ -83,7 +83,7 @@ bool result = sut.Process(buffer); // result == true ``` -**Custom Equality Comparers** +### Custom Equality Comparers Use `.Using(IEqualityComparer)` to provide custom equality comparison for `It.Is()` and `It.IsOneOf()`: @@ -118,7 +118,7 @@ bool result = sut.Process("test123", 5); ## Parameter Interaction -**Callbacks** +### Callbacks With `.Do`, you can register a callback for individual parameters of a method setup. This allows you to implement side effects or checks directly when the method or indexer is called. @@ -132,7 +132,7 @@ sut.Dispense("Dark", 42); // lastAmount == 42 ``` -**Monitor** +### Monitor With `.Monitor(out monitor)`, you can track the actual values passed during test execution and analyze them afterward. diff --git a/Docs/pages/special-types/02-delegates.md b/Docs/pages/special-types/02-delegates.md index 85fac4e9..1ce91beb 100644 --- a/Docs/pages/special-types/02-delegates.md +++ b/Docs/pages/special-types/02-delegates.md @@ -2,7 +2,7 @@ Mockolate supports mocking delegates including `Action`, `Func`, and custom delegates. -**Setup** +## Setup Use `SetupMock.Delegate(…)` to configure delegate behavior. @@ -46,7 +46,7 @@ processor.SetupMock.Delegate(It.IsAny(), It.IsRef(v => v + 1), It.IsOu - Full [parameter matching](https://awexpect.com/docs/mockolate/setup#parameter-matching) support for delegate parameters including `ref` and `out` parameters. -**Verification** +## Verification You can verify that delegates were invoked with specific arguments: diff --git a/README.md b/README.md index 013ccbe2..540b44f7 100644 --- a/README.md +++ b/README.md @@ -267,11 +267,13 @@ sut.SetupMock.Method.Dispense(It.Is("Green"), It.IsAny()) **Async Methods** -For `Task` or `ValueTask` methods, use `.ReturnsAsync(…)`: +For `Task` or `ValueTask` methods, use `.ReturnsAsync(…)` or `ThrowsAsync(…)`: ```csharp sut.SetupMock.Method.DispenseAsync(It.IsAny(), It.IsAny()) - .ReturnsAsync(true); + .ReturnsAsync((_, v) => v) // First execution returns the value of the `int` parameter + .ThrowsAsync(new TimeoutException()) // Second execution throws a TimeoutException; + .ReturnsAsync(0).Forever(); // Subsequent executions return 0 ``` ### Indexers From d2ca323d1535a84e533d684678571101ceb5712a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 31 Jan 2026 12:53:21 +0100 Subject: [PATCH 2/2] Fix review issues --- Docs/pages/setup/02-methods.md | 4 ++-- README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Docs/pages/setup/02-methods.md b/Docs/pages/setup/02-methods.md index 37820c42..731be760 100644 --- a/Docs/pages/setup/02-methods.md +++ b/Docs/pages/setup/02-methods.md @@ -43,6 +43,6 @@ For `Task` or `ValueTask` methods, use `.ReturnsAsync(…)` or `ThrowsAsyn ```csharp sut.SetupMock.Method.DispenseAsync(It.IsAny(), It.IsAny()) .ReturnsAsync((_, v) => v) // First execution returns the value of the `int` parameter - .ThrowsAsync(new TimeoutException()) // Second execution throws a TimeoutException; - .ReturnsAsync(0).Forever(); // Subsequent executions return 0 + .ThrowsAsync(new TimeoutException()) // Second execution throws a TimeoutException + .ReturnsAsync(0).Forever(); // Subsequent executions return 0 ``` diff --git a/README.md b/README.md index 540b44f7..922c859d 100644 --- a/README.md +++ b/README.md @@ -272,8 +272,8 @@ For `Task` or `ValueTask` methods, use `.ReturnsAsync(…)` or `ThrowsAsyn ```csharp sut.SetupMock.Method.DispenseAsync(It.IsAny(), It.IsAny()) .ReturnsAsync((_, v) => v) // First execution returns the value of the `int` parameter - .ThrowsAsync(new TimeoutException()) // Second execution throws a TimeoutException; - .ReturnsAsync(0).Forever(); // Subsequent executions return 0 + .ThrowsAsync(new TimeoutException()) // Second execution throws a TimeoutException + .ReturnsAsync(0).Forever(); // Subsequent executions return 0 ``` ### Indexers