Moq4: Mock.Object throwing "Castle.DynamicProxy.InvalidProxyConstructorArgumentsException"

Created on 21 Feb 2018  路  2Comments  路  Source: moq/moq4

Context:
OS: Windows 10 Version 10.0.16299 Build 16299
.Net Project Version: 4.6.1
Visual Studio Version: Microsoft Visual Studio Professional 2017 Version 15.5.5
Visual Studio Project Type: MSTest/ClassLibrary
Installed Moq Nuget Version: 4.8.1

Expected Behavior:
Create a mock of a custom class named RedisConfiguration without having the object property on the mock property throw an exception.

Steps For Reproducing Behavior
Create a new MSTest project with the custom class RedisConfiguration defined as in the RedisConfiguration.txt

The test project should look like
KeyLoggingConsumerService.txt

Set a breakpoint on the line right after the statement:
RedisConfigurationMock = new Mock<RedisConfiguration>();

Debug the test. Navigate to the the Object property on the RedisConfigurationMock instance.

Observe the error that is thrown. It should be Castle.DynamicProxy.InvalidProxyConstructorArgumentsException

Most helpful comment

RedisConfiguration's constructors are parameterized, so you need to pass the ctor arguments via new Mock<RedisConfiguration>(argsForRedisConfigurationCtor). You couldn't do new RedisConfiguration() either, right? ;-)

All 2 comments

RedisConfiguration's constructors are parameterized, so you need to pass the ctor arguments via new Mock<RedisConfiguration>(argsForRedisConfigurationCtor). You couldn't do new RedisConfiguration() either, right? ;-)

You were correct that I needed to pass in the arguments for the RedisConfiguration into the Mock object. Thank you.

Was this page helpful?
0 / 5 - 0 ratings