diff --git a/Docs/pages/01-create-mocks.md b/Docs/pages/01-create-mocks.md index 7af4a971..f8ac3bca 100644 --- a/Docs/pages/01-create-mocks.md +++ b/Docs/pages/01-create-mocks.md @@ -58,6 +58,55 @@ var classMock = Mock.Create( - Tuples with recursively defaulted values - `null` for other reference types +### Advanced Mock Behavior + +#### Custom Default Value Factories + +Provide custom default values for specific types using `.WithDefaultValueFor()`: + +```csharp +var behavior = MockBehavior.Default + .WithDefaultValueFor(() => "default") + .WithDefaultValueFor(() => 42); + +var sut = Mock.Create(behavior); +``` + +This is useful when you want mocks to return specific default values for certain types instead of the standard defaults (e.g., `null`, `0`, empty strings). + +#### Initialize with Counter + +Initialize mocks with a counter to track creation order or assign unique identifiers: + +```csharp +var behavior = MockBehavior.Default + .Initialize((count, setup) => + { + setup.Property.TotalDispensed.InitializeWith(count * 10); + }); + +var mock1 = Mock.Create(behavior); +var mock2 = Mock.Create(behavior); + +// mock1.TotalDispensed == 10 +// mock2.TotalDispensed == 20 +``` + +The counter starts at 1 and increments with each mock instance created. This is particularly useful when you need to create multiple mocks with slightly different configurations. + +#### Constructor Parameters Configuration + +Configure constructor parameters for specific types at the behavior level: + +```csharp +var behavior = MockBehavior.Default + .UseConstructorParametersFor("Dark", 100); + +var sut = Mock.Create(behavior); +``` + +This allows you to define default constructor parameters in the behavior, which will be used when creating mocks unless explicit parameters are provided via `BaseClass.WithConstructorParameters()`. + ## Using a factory for shared behavior Use `Mock.Factory` to create multiple mocks with a shared behavior: diff --git a/README.md b/README.md index 38611181..e2a19955 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,55 @@ var classMock = Mock.Create( - Tuples with recursively defaulted values - `null` for other reference types +### Advanced Mock Behavior + +#### Custom Default Value Factories + +Provide custom default values for specific types using `.WithDefaultValueFor()`: + +```csharp +var behavior = MockBehavior.Default + .WithDefaultValueFor(() => "default") + .WithDefaultValueFor(() => 42); + +var sut = Mock.Create(behavior); +``` + +This is useful when you want mocks to return specific default values for certain types instead of the standard defaults (e.g., `null`, `0`, empty strings). + +#### Initialize with Counter + +Initialize mocks with a counter to track creation order or assign unique identifiers: + +```csharp +var behavior = MockBehavior.Default + .Initialize((count, setup) => + { + setup.Property.TotalDispensed.InitializeWith(count * 10); + }); + +var mock1 = Mock.Create(behavior); +var mock2 = Mock.Create(behavior); + +// mock1.TotalDispensed == 10 +// mock2.TotalDispensed == 20 +``` + +The counter starts at 1 and increments with each mock instance created. This is particularly useful when you need to create multiple mocks with slightly different configurations. + +#### Constructor Parameters Configuration + +Configure constructor parameters for specific types at the behavior level: + +```csharp +var behavior = MockBehavior.Default + .UseConstructorParametersFor("Dark", 100); + +var sut = Mock.Create(behavior); +``` + +This allows you to define default constructor parameters in the behavior, which will be used when creating mocks unless explicit parameters are provided via `BaseClass.WithConstructorParameters()`. + ### Using a factory for shared behavior Use `Mock.Factory` to create multiple mocks with a shared behavior: