単体テストで Moq に問題が発生しましたが、どこが間違っているのかわかりません。インターフェイスに次のようなメソッドがあります。
void WriteToRegistryKey (String key, Object value);
そして、私は次のように単体テストしています:
var testRegistry = new Mock<IRegistry>();
testRegistry.Setup(x => x.WriteToRegistryKey(It.IsAny<string>(), It.IsAny<int>()));
Utility testUtility = new ConfigUtil(testRegistry.Object);
testUtility.UpdateRegistry();
testRegistry.Verify(x => x.WriteToRegistryKey("MaxNumLogFiles", 10));
内部で testUtility.UpdateRegistry() を呼び出すと、WriteToRegistryKey が呼び出されます。WriteToRegistryKey メソッドが正しい値を渡すことをテストしたいと思います。
ただし、テストを実行すると次のように表示されます。
Moq.MockException :
Expected invocation on the mock at least once, but was never performed: x => x.WriteToRegistryKey("MaxNumLogFiles", (Object)10)
Configured setups:
x => x.WriteToRegistryKey(It.IsAny<String>(), It.IsAny<Int32>()), Times.Never
Performed invocations:
IRegistry.WriteToRegistryKey("MaxNumLogFiles", 10)
testRegistry.Verify を次のように変更すると:
testRegistry.Verify(x => x.WriteToRegistryKey("MaxNumLogFiles", It.IsAny<object>()));
それは機能するので、問題は WriteToRegistryKey メソッドが取る 2 番目のパラメーターと、int とオブジェクトの違いにあるようですが、私はそれを理解できないようです。
助けてくれてありがとう!