Verificando o método genérico chamado usando o Moq
Estou tendo problemas para verificar essa simulaçãoIInterface.SomeMethod<T>(T arg)
foi chamado usandoMoq.Mock.Verify
.
Eu posso verificar se o método foi chamado em uma interface "Padrão" usandoIt.IsAny<IGenericInterface>()
ouIt.IsAny<ConcreteImplementationOfIGenericInterface>()
, e não tenho problemas para verificar uma chamada de método genérico usandoIt.IsAny<ConcreteImplementationOfIGenericInterface>()
, mas não posso verificar se um método genérico foi chamado usandoIt.IsAny<IGenericInterface>()
- sempre diz que o método não foi chamado e o teste da unidade falha.
Aqui está o meu teste de unidade:
public void TestMethod1()
{
var mockInterface = new Mock<IServiceInterface>();
var classUnderTest = new ClassUnderTest(mockInterface.Object);
classUnderTest.Run();
// next three lines are fine and pass the unit tests
mockInterface.Verify(serviceInterface => serviceInterface.NotGenericMethod(It.IsAny<ConcreteSpecificCommand>()), Times.Once());
mockInterface.Verify(serviceInterface => serviceInterface.NotGenericMethod(It.IsAny<ISpecificCommand>()), Times.Once());
mockInterface.Verify(serviceInterface => serviceInterface.GenericMethod(It.IsAny<ConcreteSpecificCommand>()), Times.Once());
// this line breaks: "Expected invocation on the mock once, but was 0 times"
mockInterface.Verify(serviceInterface => serviceInterface.GenericMethod(It.IsAny<ISpecificCommand>()), Times.Once());
}
Aqui está minha aula em teste:
public class ClassUnderTest
{
private IServiceInterface _service;
public ClassUnderTest(IServiceInterface service)
{
_service = service;
}
public void Run()
{
var command = new ConcreteSpecificCommand();
_service.GenericMethod(command);
_service.NotGenericMethod(command);
}
}
Aqui está o meuIServiceInterface
:
public interface IServiceInterface
{
void NotGenericMethod(ISpecificCommand command);
void GenericMethod<T>(T command);
}
E aqui está minha hierarquia de herança de interface / classe:
public interface ISpecificCommand
{
}
public class ConcreteSpecificCommand : ISpecificCommand
{
}