We are currently investigating if we should use Natural mocks instead of Reflective mocks. The simple syntax and the ability to mock "chained calls" are really nice. However, one of our tests failed when we changed it from using reflective mocks to natural mocks.
The test can easily be reproduced. Just mock System.Data.OracleClient.OracleConnection like this:
[TestMethod, VerifyMocks]
public void OracleConnectionNatural() {
using (RecordExpectations r = new RecordExpectations()) {
OracleConnection moc = new OracleConnection();
}
OracleConnection oc = new OracleConnection();
}
When the test is run an exception is thrown:
System.TypeInitializationException: The type initializer for 'System.Data.OracleClient.OracleConnection' threw an exception. ---> System.TypeLoadException: Method 'GetConnectionPoolGroup' on type 'MockDbConnectionFactory' from assembly 'DynamicMockAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a0857b23fb3f20d9' is overriding a method that is not visible from that assembly..
at System.Reflection.Emit.TypeBuilder._TermCreateClass(Int32 handle, Module module)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at az.a(Type A_0, Object[] A_1)
at TypeMock.MockManager.MockObject(Type type, Constructor mockConstructors, Object[] args)
at TypeMock.MockManager.MockObject(Type type, Object[] args)
at TypeMock.Mock.b(String A_0)
at TypeMock.Mock.h()
at TypeMock.RecorderManager.a(String A_0, String A_1, Object A_2, Type A_3, Boolean A_4)
at TypeMock.RecorderManager.a(String A_0, String A_1, Object A_2, Object[] A_3, MethodBase A_4, Object A_5, StackTrace A_6)
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at System.Data.OracleClient.OracleConnection..cctor()
--- End of inner exception stack trace ---
at System.Data.OracleClient.OracleConnection..ctor()
at LibraryTest.QueueTest.OracleConnection() in C:workspacesTypeMock2005LibraryTestQueueTest.cs:line 97
at TypeMock.VerifyMocksAttribute.Execute()
at TypeMock.MethodDecorator.e()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
I'm aware that the example is not really useful as a test, but I have removed the test code itself to illustrate the problem. Just mocking the constructor is enough to trigger the exception. The odd thing is that when we use reflective mocks to mock the OracleConnection we don't see the problem:
[TestMethod, VerifyMocks]
public void OracleConnectionReflective() {
Mock moc = MockManager.Mock<OracleConnection>(Constructor.Mocked);
OracleConnection oc = new OracleConnection();
}
Please comment.