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..aafa3981 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:** @@ -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 @@ -61,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 0cf8e6d5..f4868326 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:** @@ -119,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 @@ -134,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