Using Verify to confirm expected parameter values in Moq mock class

NunitMoqAutofixture

Nunit Problem Overview


I'm trying to verify that a method within a mock is called with an expected object parameter. I'm using Moq, nUnit, and thinking that AutoFixture's Likeness should get the job done. Below is a simplified version of what i'm trying to do.

Is there a way to do this with AutoFixture? Is there a better way to verify that Something is called with the appropriate parameter?

Overriding Equals in the A class to compare the property values and changing the Verify line to:

barMock.Verify(m => m.Something(a));

passes, however I'd rather not override Equals in every class like A in my project.

namespace Test
{
    using Moq;
    using NUnit.Framework;
    using Ploeh.SemanticComparison.Fluent;

    public class A
    {
        public int P1 { get; set; }
    }
    public interface IBar
    {
        void Something(A a);
    }

    public class Foo
    {
        public A Data { get; private set; }
        public void DoSomethingWith(IBar bar)
        {
            Data = new A { P1 = 1 };
            bar.Something(Data);
        }
    }

    [TestFixture]
    public class AutoFixtureTest
    {
        [Test]
        public void TestSample()
        {
            var foo = new Foo();
            var barMock = new Mock<IBar>();
            var a = new A { P1 = 1 };
            var expectedA = a.AsSource().OfLikeness<A>();

            foo.DoSomethingWith(barMock.Object);

            expectedA.ShouldEqual(foo.Data);   // passes
            barMock.Verify(m => m.Something(expectedA.Value));  // fails
        }
    }
}

Nunit Solutions


Solution 1 - Nunit

In Verify Moq by default checks reference equality for arguments so it only passes when you provide the same instances (except if you've overriden Equals) in your tests and in your implementation.

In you case the expectedA.Value just returns the new A { P1 = 1 } created in the test which, of course, isn't the same instance created in DoSomethingWith.

You need to use Moq's It.Is construct to properly test this without overriding Equals (in fact for this you don't need Autofixture at all):

barMock.Verify(m => m.Something(It.Is<A>(arg => arg.P1 == a.P1)));

But if you have multiple properties like P1,P2,P3... AutoFixture can be useful:

barMock.Verify(m => m.Something(It.Is<A>(arg => expectedA.Equals(a))));

Because you don't need to write out the equality checks manually for all the properties.

Solution 2 - Nunit

If you upgrade to AutoFixture 2.9.1 (or newer) you can call the CreateProxy method on the Likeness instance which will emit a dynamic proxy for the destination type.

The generated dynamic proxy overrides Equals using Likeness which simplifies the syntax (quite a lot).

Here is the original test method, modified to use the Likeness proxy:

[Test]
public void TestSample()
{
    var foo = new Foo();
    var barMock = new Mock<IBar>();
    var expected = new A().AsSource().OfLikeness<A>().CreateProxy();
    expected.P1 = 1;

    foo.DoSomethingWith(barMock.Object);

    Assert.True(expected.Equals(foo.Data));     // passes
    barMock.Verify(m => m.Something(expected)); // passes
}

Note that it also makes the test assertion much more specific than accepting Any instance.

You can find more details on this new feature here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjamintoView Question on Stackoverflow
Solution 1 - NunitnemesvView Answer on Stackoverflow
Solution 2 - NunitNikos BaxevanisView Answer on Stackoverflow