> Mike Valenty

Moq Extension Methods for Unity

| Comments

A few people have asked about the RegisterMock extension method used in another post. The usage looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Test]
public void Should_delete_removed_image()
{
    container.RegisterMock<IFileRepository>()
        .Setup(r => r.Delete(It.IsAny<IFile>()))
        .Verifiable();

    container.RegisterMock<IBusinessRepository>()
        .Setup(r => r.FindById(3))
        .Returns(CreateBusinessWith(new BusinessImage { ImageId = 4 }));

    var controller = container.Resolve<BusinessGalleryController>();
    controller.Delete(3, 4);

    container.VerifyMockFor<IFileRepository>();
}

It’s just a few helper extensions for using Moq with Unity that cut down on the noise in tests. My friend Keith came up with it, I just happen to blog about it first. Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static class MoqExtensions
{
    public static Mock<T> RegisterMock<T>(this IUnityContainer container) where T : class
    {
        var mock = new Mock<T>();

        container.RegisterInstance<Mock<T>>(mock);
        container.RegisterInstance<T>(mock.Object);

        return mock;
    }

    /// <summary>
    /// Use this to add additional setups for a mock that is already registered
    /// </summary>
    public static Mock<T> ConfigureMockFor<T>(this IUnityContainer container) where T : class
    {
        return container.Resolve<Mock<T>>();
    }

    public static void VerifyMockFor<T>(this IUnityContainer container) where T : class
    {
        container.Resolve<Mock<T>>().VerifyAll();
    }
}

Comments