1

I am experiencing an issue with Moq in my unit testing and I am not sure where I am going wrong. I have a method in my interface with like this:

void WriteToRegistryKey (String key, Object value);

and I am unit testing it like this:

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));

Under the hood when I call testUtility.UpdateRegistry() it calls my WriteToRegistryKey I want to test that WriteToRegistryKey method passing in the correct values.

However I receive this when I run the test:

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)

If I change my testRegistry.Verify to:

testRegistry.Verify(x => x.WriteToRegistryKey("MaxNumLogFiles", It.IsAny<object>()));

it works, so the issue seems to be around the second parameter the WriteToRegistryKey method takes, and the difference between int and object, but I cant seem to figure it out.

Thanks for all help!

3
  • 1
    In the signature of WriteToRegistryKey you have Object as the Type for value....thus anyone passing an int "value" to your WriteRegistryKey it will be implicitly boxed (i.e. become an object)...I guess this is affecting the Verify. Commented Aug 2, 2012 at 16:11
  • I think the issue is around this area to, I don't want to change anything about the WriteToRegistryKey method though, it must be possible to test it as it is somehow using Moq... Commented Aug 2, 2012 at 16:25
  • testRegistry.Setup(x => x.WriteToRegistryKey(It.IsAny<string>(), It.IsAny<object>( v => (v is int) ))); ... don't know if that will work....or do something similar in the lambda expression. Commented Aug 2, 2012 at 16:44

1 Answer 1

3

It would be helpful to see the body of implementation of testUtility.UpdateRegistry(); how the .WriteToRegistryKey method is called there.

However: I would remove the line where you setup the testRegistry mock:
testRegistry.Setup(x => x.WriteToRegistryKey(It.IsAny<string>(), It.IsAny<int>())); Because, you want to test it, if it was called with correct arguments or not. There is no reason to setup that with Moq.

and if your test passes with testRegistry.Verify(x => x.WriteToRegistryKey("MaxNumLogFiles", It.IsAny<object>()));

It could mean two things:

  1. Your WriteToRegistryKey method is called with other then 10 value - error in UpdateRegistry method
  2. or it's null, because you setup it with:

It.IsAny<string>(), It.IsAny<int>()

When you use It.IsAny<type>() it could be also null.

Sign up to request clarification or add additional context in comments.

1 Comment

Some very helpful information in here thats helped me understand the tests better, thank you Adronius.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.