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
35 changes: 34 additions & 1 deletion Docs/pages/setup/01-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sut.SetupMock.Property.TotalDispensed.InitializeWith(42);

**Returns / Throws**

Alternatively, set up properties with `Returns` and `Throws` (supports sequences):
Set up properties with `Returns` and `Throws` (supports sequences):

```csharp
sut.SetupMock.Property.TotalDispensed
Expand All @@ -23,6 +23,13 @@ sut.SetupMock.Property.TotalDispensed
.Returns(4);
```

You can also return a value based on the previous value:

```csharp
sut.SetupMock.Property.TotalDispensed
.Returns((current) => current + 10); // Increment by 10 each read
```

**Callbacks**

Register callbacks on the setter or getter:
Expand All @@ -31,3 +38,29 @@ Register callbacks on the setter or getter:
sut.SetupMock.Property.TotalDispensed.OnGet.Do(() => Console.WriteLine("TotalDispensed was read!"));
sut.SetupMock.Property.TotalDispensed.OnSet.Do((oldValue, newValue) => Console.WriteLine($"Changed from {oldValue} to {newValue}!") );
```

Callbacks can also receive the invocation counter and current value:

```csharp
// Getter with counter and current value
sut.SetupMock.Property.TotalDispensed
.OnGet.Do((int count, int value) =>
Console.WriteLine($"Read #{count}, current value: {value}"));

// Setter with counter and new value
sut.SetupMock.Property.TotalDispensed
.OnSet.Do((int count, int newValue) =>
Console.WriteLine($"Set #{count} to {newValue}"));
```

**Register**

Register a setup without providing a value (useful with `ThrowWhenNotSetup`):

```csharp
var strictMock = Mock.Create<IChocolateDispenser>(
new MockBehavior { ThrowWhenNotSetup = true });

// Register property without value - won't throw
strictMock.SetupMock.Property.TotalDispensed.Register();
```
20 changes: 18 additions & 2 deletions Docs/pages/setup/03-indexers.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,24 @@ sut.SetupMock.Indexer(It.Is("Dark"))

- `.InitializeWith(…)` can take a value or a callback with parameters.
- `.Returns(…)` and `.Throws(…)` support direct values, callbacks, and callbacks with parameters and/or the current
value.
- `.OnGet.Do(…)` and `.OnSet.Do(…)` support callbacks with or without parameters.
value. You can also return a value based on the previous value:
```csharp
sut.SetupMock.Indexer(It.Is("Dark"))
.Returns((string type, int current) => current + 10); // Increment by 10 each read
```
- `.OnGet.Do(…)` and `.OnSet.Do(…)` support callbacks with or without parameters. Callbacks can receive the invocation
counter, indexer parameters, and current value:
```csharp
// Getter with counter, parameter, and current value
sut.SetupMock.Indexer(It.IsAny<string>())
.OnGet.Do((int count, string type, int value) =>
Console.WriteLine($"Read #{count} for {type}, current value: {value}"));

// Setter with counter, parameter, and new value
sut.SetupMock.Indexer(It.IsAny<string>())
.OnSet.Do((int count, string type, int newValue) =>
Console.WriteLine($"Set #{count} for {type} to {newValue}"));
```
- `.Returns(…)` and `.Throws(…)` can be chained to define a sequence of behaviors, which are cycled through on each
call.
- Use `.SkippingBaseClass(…)` to override the base class behavior for a specific indexer (only for class mocks).
Expand Down
55 changes: 52 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ sut.SetupMock.Property.TotalDispensed.InitializeWith(42);

**Returns / Throws**

Alternatively, set up properties with `Returns` and `Throws` (supports sequences):
Set up properties with `Returns` and `Throws` (supports sequences):

```csharp
sut.SetupMock.Property.TotalDispensed
Expand All @@ -203,6 +203,13 @@ sut.SetupMock.Property.TotalDispensed
.Returns(4);
```

You can also return a value based on the previous value:

```csharp
sut.SetupMock.Property.TotalDispensed
.Returns((current) => current + 10); // Increment by 10 each read
```

**Callbacks**

Register callbacks on the setter or getter:
Expand All @@ -212,6 +219,32 @@ sut.SetupMock.Property.TotalDispensed.OnGet.Do(() => Console.WriteLine("TotalDis
sut.SetupMock.Property.TotalDispensed.OnSet.Do((oldValue, newValue) => Console.WriteLine($"Changed from {oldValue} to {newValue}!") );
```

Callbacks can also receive the invocation counter and current value:

```csharp
// Getter with counter and current value
sut.SetupMock.Property.TotalDispensed
.OnGet.Do((int count, int value) =>
Console.WriteLine($"Read #{count}, current value: {value}"));

// Setter with counter and new value
sut.SetupMock.Property.TotalDispensed
.OnSet.Do((int count, int newValue) =>
Console.WriteLine($"Set #{count} to {newValue}"));
```

**Register**

Register a setup without providing a value (useful with `ThrowWhenNotSetup`):

```csharp
var strictMock = Mock.Create<IChocolateDispenser>(
new MockBehavior { ThrowWhenNotSetup = true });

// Register property without value - won't throw
strictMock.SetupMock.Property.TotalDispensed.Register();
```

### Methods

Use `mock.SetupMock.Method.MethodName(…)` to set up methods. You can specify argument matchers for each parameter.
Expand Down Expand Up @@ -275,8 +308,24 @@ sut.SetupMock.Indexer(It.Is("Dark"))

- `.InitializeWith(…)` can take a value or a callback with parameters.
- `.Returns(…)` and `.Throws(…)` support direct values, callbacks, and callbacks with parameters and/or the current
value.
- `.OnGet.Do(…)` and `.OnSet.Do(…)` support callbacks with or without parameters.
value. You can also return a value based on the previous value:
```csharp
sut.SetupMock.Indexer(It.Is("Dark"))
.Returns((string type, int current) => current + 10); // Increment by 10 each read
```
- `.OnGet.Do(…)` and `.OnSet.Do(…)` support callbacks with or without parameters. Callbacks can receive the invocation
counter, indexer parameters, and current value:
```csharp
// Getter with counter, parameter, and current value
sut.SetupMock.Indexer(It.IsAny<string>())
.OnGet.Do((int count, string type, int value) =>
Console.WriteLine($"Read #{count} for {type}, current value: {value}"));

// Setter with counter, parameter, and new value
sut.SetupMock.Indexer(It.IsAny<string>())
.OnSet.Do((int count, string type, int newValue) =>
Console.WriteLine($"Set #{count} for {type} to {newValue}"));
```
- `.Returns(…)` and `.Throws(…)` can be chained to define a sequence of behaviors, which are cycled through on each
call.
- Use `.SkippingBaseClass(…)` to override the base class behavior for a specific indexer (only for class mocks).
Expand Down