Skip to content
Merged
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
23 changes: 13 additions & 10 deletions Docs/pages/00-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<IChocolateDispenser>();

Expand All @@ -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
Expand All @@ -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;
}
```
13 changes: 12 additions & 1 deletion Docs/pages/01-create-mocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var classWithArgsMock = Mock.Create<MyChocolateDispenserWithCtor>(
);

// Specify up to 8 additional interfaces for the mock:
var sut2 = factory.Create<MyChocolateDispenser, ILemonadeDispenser>();
var sut2 = Mock.Create<MyChocolateDispenser, ILemonadeDispenser>();
```

**Notes:**
Expand Down Expand Up @@ -47,8 +47,16 @@ var classMock = Mock.Create<MyChocolateDispenser>(
- 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<T>(params Action<IMockSetup<T>>[] 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<T>`, `List<T>`, etc.)
- Empty string for `string`
- Completed tasks for `Task`, `Task<T>`, `ValueTask` and `ValueTask<T>`
- Tuples with recursively defaulted values
- `null` for other reference types

## Using a factory for shared behavior

Expand All @@ -61,3 +69,6 @@ var factory = new Mock.Factory(behavior);
var sut1 = factory.Create<IChocolateDispenser>();
var sut2 = factory.Create<ILemonadeDispenser>();
```

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.
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<IChocolateDispenser>();

Expand All @@ -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
Expand All @@ -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
Expand All @@ -88,7 +91,7 @@ var classWithArgsMock = Mock.Create<MyChocolateDispenserWithCtor>(
);

// Specify up to 8 additional interfaces for the mock:
var sut2 = factory.Create<MyChocolateDispenser, ILemonadeDispenser>();
var sut2 = Mock.Create<MyChocolateDispenser, ILemonadeDispenser>();
```

**Notes:**
Expand Down Expand Up @@ -119,8 +122,16 @@ var classMock = Mock.Create<MyChocolateDispenser>(
- 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<T>(params Action<IMockSetup<T>>[] 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<T>`, `List<T>`, etc.)
- Empty string for `string`
- Completed tasks for `Task`, `Task<T>`, `ValueTask` and `ValueTask<T>`
- Tuples with recursively defaulted values
- `null` for other reference types

### Using a factory for shared behavior

Expand All @@ -134,6 +145,9 @@ var sut1 = factory.Create<IChocolateDispenser>();
var sut2 = factory.Create<ILemonadeDispenser>();
```

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
Expand Down
Loading