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
59 changes: 59 additions & 0 deletions Docs/pages/advanced-features/01-working-with-protected-members.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Working with protected members

Mockolate allows you to set up and verify protected virtual members on class mocks. Access protected members using the
`.Protected` property:

## Example:

```csharp
public abstract class ChocolateDispenser
{
protected virtual bool DispenseInternal(string type, int amount) => true;
protected virtual int InternalStock { get; set; }
}

var sut = Mock.Create<ChocolateDispenser>();
```

### Setup

```csharp
// Setup protected method
sut.SetupMock.Protected.Method.DispenseInternal(
It.Is("Dark"), It.IsAny<int>())
.Returns(true);

// Setup protected property
sut.SetupMock.Protected.Property.InternalStock.InitializeWith(100);
```

**Notes:**

- Protected members can be set up and verified just like public members, using the `.Protected` accessor.
- All setup options (`.Returns()`, `.Throws()`, `.Do()`, `.InitializeWith()`, etc.) work with protected members.

### Verification

```csharp
// Verify protected method was invoked
sut.VerifyMock.Invoked.Protected.DispenseInternal(
It.Is("Dark"), It.IsAny<int>()).Once();

// Verify protected property was read
sut.VerifyMock.Got.Protected.InternalStock().AtLeastOnce();

// Verify protected property was set
sut.VerifyMock.Set.Protected.InternalStock(It.Is(100)).Once();

// Verify protected indexer was read
sut.VerifyMock.GotProtectedIndexer(It.Is(0)).Once();

// Verify protected indexer was set
sut.VerifyMock.SetProtectedIndexer(It.Is(0), It.Is(42)).Once();
```

**Note:**

- All verification options (argument matchers, count assertions) work the same for protected members as for public
members.
- Protected indexers are supported using `.GotProtectedIndexer()`/`.SetProtectedIndexer()` for verification.
2 changes: 2 additions & 0 deletions Mockolate.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
<File Path="Docs/pages/08-comparison.md" />
</Folder>
<Folder Name="/_/Docs/advanced-features/">
<File Path="Docs/pages/advanced-features/01-working-with-protected-members.md" />
<File Path="Docs/pages/advanced-features/02-advanced-callback-features.md" />
<File Path="Docs/pages/advanced-features/_category_.json" />
</Folder>
<Folder Name="/_/Docs/setup/">
Expand Down
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,66 @@ If the order is incorrect or a call is missing, a `MockVerificationException` wi

## Advanced Features

### Working with protected members

Mockolate allows you to set up and verify protected virtual members on class mocks. Access protected members using the
`.Protected` property:

#### Example:

```csharp
public abstract class ChocolateDispenser
{
protected virtual bool DispenseInternal(string type, int amount) => true;
protected virtual int InternalStock { get; set; }
}

var sut = Mock.Create<ChocolateDispenser>();
```

##### Setup

```csharp
// Setup protected method
sut.SetupMock.Protected.Method.DispenseInternal(
It.Is("Dark"), It.IsAny<int>())
.Returns(true);

// Setup protected property
sut.SetupMock.Protected.Property.InternalStock.InitializeWith(100);
```

**Notes:**

- Protected members can be set up and verified just like public members, using the `.Protected` accessor.
- All setup options (`.Returns()`, `.Throws()`, `.Do()`, `.InitializeWith()`, etc.) work with protected members.

##### Verification

```csharp
// Verify protected method was invoked
sut.VerifyMock.Invoked.Protected.DispenseInternal(
It.Is("Dark"), It.IsAny<int>()).Once();

// Verify protected property was read
sut.VerifyMock.Got.Protected.InternalStock().AtLeastOnce();

// Verify protected property was set
sut.VerifyMock.Set.Protected.InternalStock(It.Is(100)).Once();

// Verify protected indexer was read
sut.VerifyMock.GotProtectedIndexer(It.Is(0)).Once();

// Verify protected indexer was set
sut.VerifyMock.SetProtectedIndexer(It.Is(0), It.Is(42)).Once();
```

**Note:**

- All verification options (argument matchers, count assertions) work the same for protected members as for public
members.
- Protected indexers are supported using `.GotProtectedIndexer()`/`.SetProtectedIndexer()` for verification.

### Advanced callback features

#### Conditional callbacks
Expand Down
Loading