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
  • Any instance of the parameter type
  • Specific instance of the parameter type
  • Arguments matching certain patterns
  1. Moq

Method arguments

When configuring method calls, it's important to set proper expectations on the incoming arguments. Developers can set expectations on incoming parameters for each method call setup. If the expectation on the parameter are not matched, the setup will not be used for the specific call.

Any instance of the parameter type

The most basilar expectation accepts all instances of the argument type. The following snippet configure the mock to accept calls of the DoSomething method regardless of the incoming argument.

mock.Setup(p => p.CreateUser(It.IsAny<string>()));

Note: being Moq a type-safe framework, it's impossible to provide parameters that don't match with the method signature.

Specific instance of the parameter type

On the opposite side of the spectrum, developers can specify that the call setup should be used only if the incoming parameter is equal to the one specified in the setup.

mock.Setup(p => p.CreateUser("TEST_USERNAME"));

Note that general rules for equality comparison apply: strings and value types will be compared by their actual value, reference types will be compared on their reference unless the Object.Equals has been properly overridden.

Arguments matching certain patterns

Somewhere in between "any instance" and "specific instance" there are many patterns used to compare expected arguments and actual incoming values.

It.IsRegex only accepts incoming strings matching the given regular expression pattern

mock.Setup(p => p.CreateUser(It.IsRegex("[A-Z_]+")));

It.IsInRange only accepts numbers between the two extremes of the given range

mock.Setup(p => p.GetUserById(It.IsInRange(0, 100, Moq.Range.Exclusive)));

It.Is only accepts parameters that are positively evaluated by the given predicate

mock.Setup(p => p.CreateUser(It.Is<string>(s => s.StartsWith("TEST"))));

Note that the predicate is evaluated only when the method is actually invoked and the s variable represents the incoming parameter.

PreviousQuick glance at MoqNextMethod calls

Last updated 4 years ago