From 5c2bb454316f55d8de2094f940321e8e7f63f388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Thu, 18 Dec 2025 20:25:38 +0100 Subject: [PATCH 1/3] docs: improve documentation --- Docs/pages/00-index.md | 23 +++++++++++++---------- Docs/pages/01-create-mocks.md | 2 +- README.md | 25 ++++++++++++++----------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Docs/pages/00-index.md b/Docs/pages/00-index.md index a7a07d2e..584a739d 100644 --- a/Docs/pages/00-index.md +++ b/Docs/pages/00-index.md @@ -23,6 +23,15 @@ Framework 4.8. ```csharp using Mockolate; + public delegate void ChocolateDispensedDelegate(string type, int amount); + public interface IChocolateDispenser + { + int this[string type] { get; set; } + int TotalDispensed { get; set; } + bool Dispense(string type, int amount); + event ChocolateDispensedDelegate ChocolateDispensed; + } + // Create a mock for IChocolateDispenser var sut = Mock.Create(); @@ -44,7 +53,10 @@ Framework 4.8. // Track dispensed amount via event int dispensedAmount = 0; - sut.ChocolateDispensed += (type, amount) => dispensedAmount += amount; + sut.ChocolateDispensed += (type, amount) + { + dispensedAmount += amount; + } // Act: Try to dispense chocolates bool gotChoc1 = sut.Dispense("Dark", 4); // true @@ -56,13 +68,4 @@ Framework 4.8. // Output: "Dispensed amount: 9. Got chocolate? True, True, False" Console.WriteLine($"Dispensed amount: {dispensedAmount}. Got chocolate? {gotChoc1}, {gotChoc2}, {gotChoc3}"); - - public delegate void ChocolateDispensedDelegate(string type, int amount); - public interface IChocolateDispenser - { - int this[string type] { get; set; } - int TotalDispensed { get; set; } - bool Dispense(string type, int amount); - event ChocolateDispensedDelegate ChocolateDispensed; - } ``` diff --git a/Docs/pages/01-create-mocks.md b/Docs/pages/01-create-mocks.md index 36e5ddeb..7c5df8e3 100644 --- a/Docs/pages/01-create-mocks.md +++ b/Docs/pages/01-create-mocks.md @@ -16,7 +16,7 @@ var classWithArgsMock = Mock.Create( ); // Specify up to 8 additional interfaces for the mock: -var sut2 = factory.Create(); +var sut2 = Mock.Create(); ``` **Notes:** diff --git a/README.md b/README.md index 0cf8e6d5..8af9a73d 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,15 @@ Framework 4.8. ```csharp using Mockolate; + public delegate void ChocolateDispensedDelegate(string type, int amount); + public interface IChocolateDispenser + { + int this[string type] { get; set; } + int TotalDispensed { get; set; } + bool Dispense(string type, int amount); + event ChocolateDispensedDelegate ChocolateDispensed; + } + // Create a mock for IChocolateDispenser var sut = Mock.Create(); @@ -47,7 +56,10 @@ Framework 4.8. // Track dispensed amount via event int dispensedAmount = 0; - sut.ChocolateDispensed += (type, amount) => dispensedAmount += amount; + sut.ChocolateDispensed += (type, amount) + { + dispensedAmount += amount; + } // Act: Try to dispense chocolates bool gotChoc1 = sut.Dispense("Dark", 4); // true @@ -59,15 +71,6 @@ Framework 4.8. // Output: "Dispensed amount: 9. Got chocolate? True, True, False" Console.WriteLine($"Dispensed amount: {dispensedAmount}. Got chocolate? {gotChoc1}, {gotChoc2}, {gotChoc3}"); - - public delegate void ChocolateDispensedDelegate(string type, int amount); - public interface IChocolateDispenser - { - int this[string type] { get; set; } - int TotalDispensed { get; set; } - bool Dispense(string type, int amount); - event ChocolateDispensedDelegate ChocolateDispensed; - } ``` ## Create mocks @@ -88,7 +91,7 @@ var classWithArgsMock = Mock.Create( ); // Specify up to 8 additional interfaces for the mock: -var sut2 = factory.Create(); +var sut2 = Mock.Create(); ``` **Notes:** From 53d2602a5a196dd2fc931ec5d58788f403ae0f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Thu, 18 Dec 2025 20:54:17 +0100 Subject: [PATCH 2/3] Fix `MockBehavior` documentation --- Docs/pages/01-create-mocks.md | 8 ++++++++ README.md | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/Docs/pages/01-create-mocks.md b/Docs/pages/01-create-mocks.md index 7c5df8e3..93f60245 100644 --- a/Docs/pages/01-create-mocks.md +++ b/Docs/pages/01-create-mocks.md @@ -47,8 +47,16 @@ var classMock = Mock.Create( - If `false` (default), the mock will not call any base class implementations. - If `true`, the mock will call the base class implementation and use its return values as default values, if no explicit setup is defined. +- `Initialize(params Action>[] setups)`: + - Automatically initialize all mocks of type T with the given setups when they are created. - `DefaultValue` (IDefaultValueGenerator): - Customizes how default values are generated for methods/properties that are not set up. + - The default implementation provides sensible defaults for the most common use cases: + - Empty collections for collection types (e.g., `IEnumerable`, `List`, etc.) + - Empty string for `string` + - Completed tasks for `Task`, `Task`, `ValueTask` and `ValueTask` + - Tuples with recursively defaulted values + - `null` for other reference types ## Using a factory for shared behavior diff --git a/README.md b/README.md index 8af9a73d..9d253337 100644 --- a/README.md +++ b/README.md @@ -122,8 +122,16 @@ var classMock = Mock.Create( - If `false` (default), the mock will not call any base class implementations. - If `true`, the mock will call the base class implementation and use its return values as default values, if no explicit setup is defined. +- `Initialize(params Action>[] setups)`: + - Automatically initialize all mocks of type T with the given setups when they are created. - `DefaultValue` (IDefaultValueGenerator): - Customizes how default values are generated for methods/properties that are not set up. + - The default implementation provides sensible defaults for the most common use cases: + - Empty collections for collection types (e.g., `IEnumerable`, `List`, etc.) + - Empty string for `string` + - Completed tasks for `Task`, `Task`, `ValueTask` and `ValueTask` + - Tuples with recursively defaulted values + - `null` for other reference types ### Using a factory for shared behavior From 7dde8e1b80a3a1f562e6284f341e5928ffe10488 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Thu, 18 Dec 2025 21:03:09 +0100 Subject: [PATCH 3/3] Add description for a factory --- Docs/pages/01-create-mocks.md | 15 +++++++++------ README.md | 3 +++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Docs/pages/01-create-mocks.md b/Docs/pages/01-create-mocks.md index 93f60245..aafa3981 100644 --- a/Docs/pages/01-create-mocks.md +++ b/Docs/pages/01-create-mocks.md @@ -51,12 +51,12 @@ var classMock = Mock.Create( - Automatically initialize all mocks of type T with the given setups when they are created. - `DefaultValue` (IDefaultValueGenerator): - Customizes how default values are generated for methods/properties that are not set up. - - The default implementation provides sensible defaults for the most common use cases: - - Empty collections for collection types (e.g., `IEnumerable`, `List`, etc.) - - Empty string for `string` - - Completed tasks for `Task`, `Task`, `ValueTask` and `ValueTask` - - Tuples with recursively defaulted values - - `null` for other reference types + - The default implementation provides sensible defaults for the most common use cases: + - Empty collections for collection types (e.g., `IEnumerable`, `List`, etc.) + - Empty string for `string` + - Completed tasks for `Task`, `Task`, `ValueTask` and `ValueTask` + - Tuples with recursively defaulted values + - `null` for other reference types ## Using a factory for shared behavior @@ -69,3 +69,6 @@ var factory = new Mock.Factory(behavior); var sut1 = factory.Create(); var sut2 = factory.Create(); ``` + +Using a factory allows you to create multiple mocks with identical, centrally configured behavior. This is especially +useful when you need consistent mock setups across multiple tests or for different types. diff --git a/README.md b/README.md index 9d253337..f4868326 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,9 @@ var sut1 = factory.Create(); var sut2 = factory.Create(); ``` +Using a factory allows you to create multiple mocks with identical, centrally configured behavior. This is especially +useful when you need consistent mock setups across multiple tests or for different types. + ## Setup Set up return values or behaviors for methods, properties, and indexers on your mock. Control how the mock responds to