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
  • Configuring members by name
  • Configuring members using a mapping type
  1. Moq

Protected members

Unlike interfaces, classes (abstract and non) can have members that can be accessed only by their inheritors. These members are often defined "protected members" as for the visibility flag they are decorated with. When testing components accepting classes with these members, developers might need to configure methods not accessible to them.

Moq supports two ways to configure protected members. Unfortunately, none of them can ensure type safety being based on string and mapping types.

The snippets in this section will be based on the following class

public abstract class ServiceBase
{
    protected abstract void Step1();

    protected abstract void Step2(string value);

    protected abstract int Count { get; set; }

    public void DoSomething(string value)
    {
        Step1();
        Step2(value);
    }
}

Finally, please note that both approaches require importing of the Moq.Protected namespace. Also, the helpers targeting protected members are accessible via the Protected() construct.

Configuring members by name

The Protected construct offers helpers to configure members via their name.

mock.Protected()
    .Setup("Step1")
    .Callback(() => TestContext.Progress.Writeline("Step1"));

Parameters can be specified directly or via argument matching: unlike normal methods, developers should use ItExpr instead of It.

mock.Protected()
    .Setup("Step2", ItExpr.IsAny<string>())
    .Callback((string value) => TestContext.Progress.Writeline($"Step2: {value}"));

Unable to rely on static typing, Moq requires explicit typing when configuring properties.

mock.Protected()
    .SetupGet<int>("Count")
    .Returns(42);

mock.Protected()
    .SetupSet<int>("Count", ItExpr.IsAny<int>())
    .Callback((int value) => TestContext.Progress.Writeline($"Count set to {value}"));

Configuring members using a mapping type

Configuring members by name and without explicit typing can be avoided by instructing Moq to use a mapping type as reference.

The following interface mimics the protected methods exposed by the ServiceBase class.

public interface IServiceBaseMapping
{
    void Step1();
    void Step2(string value);
    int Count { get; set; }
}

Once this type is declared, it's possible to use more comfortable methods to configure the protected methods.

mock.Protected()
    .As<IServiceBaseMapping>()
    .Setup(p => p.Step1())
    .Callback(() => TestContext.Progress.Writeline("Step1"));

Parameters can be defined using It as usual.

mock.Protected()
    .As<IServiceBaseMapping>()
    .Setup(p => p.Step2(It.IsAny<string>()))
    .Callback((string value) => TestContext.Progress.Writeline($"Step2: {value}"));

Also properties are more comfortable to configure when using a mapping type

mock.Protected()
    .As<IServiceBaseMapping>()
    .SetupProperty(p => p.Count, 42);
PreviousMultiple interfacesNextGeneric methods

Last updated 4 years ago