Reputation: 133
I'm trying to verify a method call on a Moq implementing the following interface(s), but it is failing to match the invocation.
My unit test (simplified):
[Test]
public void ShouldDeleteComponent()
{
var mockDao = new Mock<IComponentDataAccess>();
Target.ComponentDao = mockDao.Object;
Target.Execute();
mockDao.Verify(x => x.Delete(It.IsAny<Component>()), Times.Once);
}
My mocked object's interfaces:
public interface IComponentDataAccess : IDataAccess<Component>
{
int Delete(Component entity);
}
public interface IDataAccess<T> where T : IEntity
{
int Delete(T entity);
}
Finally, how the code is actually called in the System Under Test:
public override void Execute()
{
DeleteItem(ComponentDao, existingComponent);
}
which calls:
protected virtual void DeleteItem<T>(IDataAccess<T> dataAccess, T item) where T : IEntity
{
dataAccess.Delete(item);
}
As you can see, the DAO is passed in as its base interface. On verification, it finds the following invocation:
Performed invocations:
IDataAccess`1.Update(blah.namespace.UserAccount)
When the invocation it's trying to match is:
IUserAccountDataAccess.Update(blah.namespace.UserAccount)
Is there a way to verify this method call with Moq?
Upvotes: 3
Views: 2475
Reputation: 8725
change:
mockDao.Verify(x => x.Delete(It.IsAny<Component>()), Times.Once);
to:
mockDao.As<IDataAccess<Component>>()
.Verify(x => x.Delete(It.IsAny<Component>()), Times.Once);
The As
method is being in use for adding other types
Upvotes: 5