2013-02-27 3 views
8

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 
{ 
} 

답변

6

그것은 현재 릴리스 버전 인 MOQ 4.0.10827에서 알려진 문제입니다. GitHub https://github.com/Moq/moq4/pull/25에서이 토론을 참조하십시오. dev 브랜치를 다운로드하여 컴파일하고 참조했다가 이제 테스트가 통과합니다.

+0

이 문제는 수정되었습니다. – arni

0

나는 그걸 날개로 갈거야. GenericMethod<T>는 T 인수가 제공 될 것을 요구하기 때문에, 할 수있을 것입니다 :

mockInterface.Verify(serviceInterface => serviceInterface.GenericMethod(It.Is<object>(x=> typeof(ISpecificCommand).IsAssignableFrom(x.GetType()))), Times.Once()); 
+0

입력 해 주셔서 감사합니다.하지만이 방법은 작동하지 않습니다. – sennett

관련 문제