Unit Testing in C#
  • Unit testing in C#
  • Unit testing
    • What to test
    • When to test
    • Qualities of a good unit test suite
    • Qualities of a good unit test
    • Dealing with dependencies
    • Running the tests
  • NUnit
    • Quick glance at NUnit
    • Creating a NUnit test project
    • Anatomy of a test fixture
    • Lifecycle of a test fixture
    • Assertions
    • Asynchronous executions
    • Parameterized tests
    • Assumptions
    • Describing your tests
  • Moq
    • Quick glance at Moq
    • Method arguments
    • Method calls
    • Properties
    • Results
    • Callbacks
    • Exceptions
    • Events
    • Verifications
    • Base class
    • Mock customization
    • Implicit mocks
    • Mock repository
    • Custom matchers
    • Multiple interfaces
    • Protected members
    • Generic methods
    • Delegates
  • AutoFixture
    • Quick glance at AutoFixture
    • Fixture
    • Create and Build
    • Type customization
    • Data annotations
    • Default configurations
    • Building custom types
    • Relays
    • Tricks
    • Idioms
    • Integration with NUnit
    • Integration with Moq
    • Combining AutoFixture with NUnit and Moq
    • Extending AutoFixture
  • Advanced topics
    • Testing HttpClient
Powered by GitBook
On this page
  • Constructor arguments
  • Preserve virtual methods' behavior
  1. Moq

Base class

Especially in older codebases, services and components can be part of a deep hierarchy of classes. Often, belonging to a class hierarchy makes it so a class inherits requirements and behaviors from its ancestors.

Each mock offers properties and constructor overloads to accommodate these requirements, such as constructor arguments, and retain the needed behaviors.

For this section, the following class will be used

public class Person 
{
    public Person(string name)
    {
        Name = name ?? throw new ArgumentNullException(nameof(name));
    }

    public string Name { get; }

    public virtual void Greet() => TestContext.Progress.Writeline($"Hello, I'm {Name}.");
}

Please notice that the method Greet is defined as virtual, thus allowing subclasses to redefine its behavior.

Constructor arguments

Generally, when mocking interfaces, there is no need to specify constructor arguments since the mock is backed by a class created with a parameterless constructor.

Unfortunately, this might not be the case when mocking classes.

In this case, developers can specify the constructor arguments when creating the mock.

var mock = new Mock<Person>("John");

The snippet above will create a mock backed by a class inheriting from Person even if it requires a string in its constructor.

Preserve virtual methods' behavior

Sometimes the classes to be mocked present one or more virtual methods whose default implementation already satisfies the needs of the mock. In cases like this it makes sense to preserve the original behavior, rather than let Moq override the method.

Moq offers two ways to achieve this.

The first one is by configuring each method accordingly by using the CallBase construct.

mock.Setup(p => p.Greet()).CallBase();

Alternatively, developers can instruct Moq to always resort to the base class behavior, whenever there is one.

var mock = new Mock<Person> { CallBase = true };

This approach is particularly useful when members of the base class are not publicly visible.

PreviousVerificationsNextMock customization

Last updated 4 years ago