Advanced Moq features

Sometimes, the default values provided by Moq might not be suitable for some test scenarios and you need to create a custom default value generation approach to complement what Moq currently provides, which are DefaultValue.Empty and DefaultValue.Mock. This can be done through extending DefaultValueProvider or LookupOrFallbackDefaultValueProvider, which are available in Moq 4.8 and higher:

public class TestDefaultValueProvider : LookupOrFallbackDefaultValueProvider{    public TestDefaultValueProvider()    {        base.Register(typeof(string), (type, mock) => string.empty);        base.Register(typeof(List<>), (type, mock) => Activator.CreateInstance(type));    }}

The TestDefaultValueProvider class created the sub-classes LookupOrFallbackDefaultValueProvider ...

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.