Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Docs/pages/01-create-mocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,55 @@ var classMock = Mock.Create<MyChocolateDispenser>(
- 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<T>()`:

```csharp
var behavior = MockBehavior.Default
.WithDefaultValueFor<string>(() => "default")
.WithDefaultValueFor<int>(() => 42);

var sut = Mock.Create<IChocolateDispenser>(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<IChocolateDispenser>((count, setup) =>
{
setup.Property.TotalDispensed.InitializeWith(count * 10);
});

var mock1 = Mock.Create<IChocolateDispenser>(behavior);
var mock2 = Mock.Create<IChocolateDispenser>(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<MyChocolateDispenser>("Dark", 100);

var sut = Mock.Create<MyChocolateDispenser>(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:
Expand Down
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,55 @@ var classMock = Mock.Create<MyChocolateDispenser>(
- 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<T>()`:

```csharp
var behavior = MockBehavior.Default
.WithDefaultValueFor<string>(() => "default")
.WithDefaultValueFor<int>(() => 42);

var sut = Mock.Create<IChocolateDispenser>(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<IChocolateDispenser>((count, setup) =>
{
setup.Property.TotalDispensed.InitializeWith(count * 10);
});

var mock1 = Mock.Create<IChocolateDispenser>(behavior);
var mock2 = Mock.Create<IChocolateDispenser>(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<MyChocolateDispenser>("Dark", 100);

var sut = Mock.Create<MyChocolateDispenser>(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:
Expand Down