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
{
}
これは修正されています。 – arni