2013-02-27 19 views
8

Moq.Mock.Verifyを使用してIInterface.SomeMethod<T>(T arg)のモックが呼び出されたことを確認するのに問題があります。Moqを使用して呼び出された汎用メソッドの確認

私は「標準」インタフェースのいずれかIt.IsAny<IGenericInterface>()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 
{ 
} 

答えて

6

それは現在のリリースバージョンです部品番号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

関連する問題