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
}
}
}
Best Answer
In
Verify
Moq by default checks reference equality for arguments so it only passes when you provide the same instances (except if you've overridenEquals
) in your tests and in your implementation.In you case the
expectedA.Value
just returns thenew A { P1 = 1 }
created in the test which, of course, isn't the same instance created inDoSomethingWith
.You need to use Moq's
It.Is
construct to properly test this without overridingEquals
(in fact for this you don't need Autofixture at all):But if you have multiple properties like P1,P2,P3... AutoFixture can be useful:
Because you don't need to write out the equality checks manually for all the properties.