Moq.Mock.Verify
을 사용하여 모의가 호출됨을 확인하는 중 문제가 발생했습니다. IInterface.SomeMethod<T>(T arg)
이 호출되었습니다.Moq를 사용하여 호출 된 일반적인 메서드 확인
나는 "표준"인터페이스 중 하나 It.IsAny<IGenericInterface>()
또는 It.IsAny<ConcreteImplementationOfIGenericInterface>()
를 사용하여 호출 된 메소드를 확인할 수있어, 나는 It.IsAny<ConcreteImplementationOfIGenericInterface>()
를 사용하여 일반 메소드 호출을 확인하는 아무 문제가 없다,하지만 일반적인 방법을 사용하여 호출 된 확인할 수 없습니다 It.IsAny<IGenericInterface>()
- 항상 메소드가 호출되지 않았고 유닛 테스트가 실패했다고합니다.
public interface IServiceInterface
{
void NotGenericMethod(ISpecificCommand command);
void GenericMethod<T>(T command);
}
그리고 여기 내 인터페이스/클래스 :
여기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 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 interface ISpecificCommand
{
}
public class ConcreteSpecificCommand : ISpecificCommand
{
}
이 문제는 수정되었습니다. – arni