Mock customization

When using the Moq framework, you can further customize mock object, to enhance the effective unit testing experience. The MockBehavior enum can be passed into Moq's Mock object constructor to specify the behavior of the mock. The enum members are Default, Strict, and Loose:

loanRepository= new Mock<ILoanRepository>(MockBehavior.Loose);

When a Loose member is selected, the mock will not throw any exceptions. The default values will always be returned. This means null will be returned for reference types, and zero or empty arrays and enumerables will be returned for value types:

loanRepository= new Mock<ILoanRepository>(MockBehavior.Strict);

Selecting a Strict member will make the mock throw exceptions for every call on ...

Get C# and .NET Core Test Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.