chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
The code listing below blows up with the specified exception. Is this the expected behavior?

SqlConnection connection = Isolate.Fake.Instance<SqlConnection>();
Isolate.WhenCalled(() => _connection.ChangeDatabase("")).IgnoreCall();


Test method <REDACTED>.UserTest.UserTest_Lookup threw exception:  System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->  System.TypeInitializationException: The type initializer for 'System.Data.ProviderBase.DbConnectionInternal' threw an exception. --->  System.NullReferenceException: Object reference not set to an instance of an object..

TypeMock.MockManager.c(Object A_0)
TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
System.Data.ProviderBase.DbConnectionInternal..cctor()
System.Data.SqlClient.SqlConnection.ChangeDatabase(String database)
b__0()
System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
System.Delegate.DynamicInvokeImpl(Object[] args)
System.Delegate.DynamicInvoke(Object[] args)
a()
c()
<REDACTED>.Mocking.DatabaseMocks.MockWithResultSet(DatabaseCall callExpectations, ResultSet resultSet)
<REDACTED>.UserTest.UserTest_Lookup()
asked by jcrivello (1.8k points)

3 Answers

0 votes
As an addendum -- it seems like TypeMock is calling the lambda expression provided to Isolate.WhenCalled(), I assume with the intention of inferring the method chain. However, it seems like it's actually calling the original method when it does so, and since SqlConnection is a fake it blows up.

-Joe
answered by jcrivello (1.8k points)
0 votes
Hi,

What happens is that when you call ChangeDatabas, the static constructor of DBInternal is called, and breaks recording. So you're basically correct in your assumption - this is a bug. :oops: Unfortunately, with the current version of AAA there is no AAA workaround - you can do that in Natural or Reflective mocks.

We'll keep you updated about the resolution of this bug.

Thanks for reporting,
answered by gilz (14.5k points)
0 votes
Hi,

I thought about it a bit more and you don't need to call IgnoreCall on the method at all, because Fake.Instance<>() already takes care of that. All void calls are ignored by default.

So you may not have a problem at all. We still do :cry:
answered by gilz (14.5k points)
...