我无法验证IInterface.SomeMethod<T>(T arg)
使用Moq.Mock.Verify
.
我可以使用It.IsAny<IGenericInterface>()
or验证在“标准”接口上调用了该方法It.IsAny<ConcreteImplementationOfIGenericInterface>()
,并且使用 验证泛型方法调用没有问题It.IsAny<ConcreteImplementationOfIGenericInterface>()
,但我无法验证使用调用泛型方法It.IsAny<IGenericInterface>()
- 它总是说该方法没有被调用并且单元测试失败。
这是我的单元测试:
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());
}
这是我正在测试的课程:
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);
}
}
这是我的IServiceInterface
:
public interface IServiceInterface
{
void NotGenericMethod(ISpecificCommand command);
void GenericMethod<T>(T command);
}
这是我的接口/类继承层次结构:
public interface ISpecificCommand
{
}
public class ConcreteSpecificCommand : ISpecificCommand
{
}