Skip to main content

Advanced features

The basics of creating, setting up and verifying mocks cover most tests. The following features help in the remaining cases:

FeatureUse it when you want to …
Protected membersset up or verify protected virtual members of a class mock
Static interface membersmock static abstract or static virtual interface members
Advanced callback featurescontrol how often and when return values and callbacks apply
Monitor interactionsonly verify the interactions within a scope, or reset the recorded interactions
Check for unexpected interactionsmake sure every interaction was verified, or every setup was used
Scenariosswitch between sets of setups, for collaborators with an internal state

For example, scenarios let a dispenser with a Refill method behave differently before and after a refill:

dispenser.Mock.InScenario("empty").Setup.Refill(It.IsAny<int>())
.Returns(true)
.TransitionTo("loaded");
dispenser.Mock.InScenario("loaded").Setup.Dispense(It.IsAny<string>(), It.IsAny<int>())
.Returns(true);