Using Verify to confirm expected parameter values in Moq mock class

jaminto picture jaminto · Jan 16, 2012 · Viewed 36.5k times · Source

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
        }
    }
}

Answer

nemesv picture nemesv · Jan 16, 2012

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.