Here is an example of mocking private methods of a class and verifying that they are called.
public class Item
{
public void Update()
{
Save();
}
private void Save()
{
Validate();
/// Save something
}
private void Validate()
{
/// Validate something
}
}
[Fact]
public void EnsureNestedPrivateMethodsAreCalled()
{
// Arrange
Item item = Mock.Create<Item>();
Mock.Arrange(() => item.Update()).CallOriginal().MustBeCalled();
Mock.NonPublic.Arrange(item, "Save").CallOriginal().MustBeCalled();
Mock.NonPublic.Arrange(item, "Validate").DoNothing().MustBeCalled();
// Act
item.Update();
// Assert
Mock.Assert(item);
}
Note that when arranging the mock you want to ensure that the original Update
and Save
methods are called. You only want to stub out functionality that you aren't testing. In this case, we are testing that calling Update
results in calls to the private members Save
and Validate
. We are not testing the implementation of the Validate
method.