 | A Single-Pass Sequence for Testing When writing unit tests for methods that operate upon IEnumerable<T> sequences, it is easy to overlook testing the behaviour for sequences that can only be enumerated once. This article describes a helper class for such tests that provides a single-pass c |
 | Mocking Multiple Interfaces with Moq Injected dependencies often implement more than one interface. These may be used by the class receiving the injected object. When testing classes with such dependencies, it can be useful to create mock objects that implement multiple interfaces. |
 | Creating Custom NUnit Assertion Methods NUnit provides a large number of assertion methods that can test values for correctness during a test run. For complex and repetitive test scenarios, it can be useful to create custom assertions that can be reused in a number of tests. |
 | Moq Partial Mocks Moq is commonly used to create mock objects based upon interfaces. However, it can also be used to mock classes. This includes the possibility of creating partial mocks, where some members are intercepted with expectations whilst others run as normal. |
 | Generate Return Values Using Lambdas in Moq Mock objects and stubs created using the Moq framework are generally used to inject dependencies with expectations that define fixed results. For more complex scenarios, lambda expressions can be used to generate results based on provided arguments. |
 | Mocking Internal Types with Moq To perform successful unit testing classes should be isolated from their dependencies. When the dependencies are internal and tests are in a separate project, this can be achieved using a combination of Moq and C# friend assemblies. |
 | Mocking Property Expectations with Moq Moq provides similar capabilities when mocking properties of classes or interfaces as when mocking methods. Although the same verification and expectation set up methods can be used in some circumstances, there are additional options for properties. |
 | Mocking Events with Moq When a class has dependencies that raise events, the reaction to those events should be unit tested. To do so, the dependencies should be isolated from the code under test with the use of test doubles. One option is to use Moq to create mock objects. |
 | Unit Testing Raised Events Many classes use events to allow messages to be passed to other classes in a loosely coupled manner. When creating unit tests for classes that use events, the raising of those events and the arguments generated should be verified. |
 | Verifying the Number of Calls to a Mocked Method The use of mock objects within unit tests allows you to verify that the correct calls to dependencies are made when a test is run. Rather than simply checking that a call is made, it is often essential to check how many times a method was executed. |