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
I have this simple test code:

MockManager.MockAll<global::System.Data.SqlClient.SqlConnection>(Constructor.Mocked);

MockManager.MockAll<global::System.Data.SqlClient.SqlCommand>(Constructor.Mocked);

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
    recorder.DefaultBehavior.CheckArguments();

    global::System.Data.SqlClient.SqlConnection connection_mocked = new global::System.Data.SqlClient.SqlConnection("");

    global::System.Data.SqlClient.SqlCommand command_mocked = new global::System.Data.SqlClient.SqlCommand("", connection_mocked);

    connection_mocked.Open();

    recorder.ExpectAndReturn(command_mocked.ExecuteNonQuery(), 0);

    command_mocked.Dispose();
    connection_mocked.Dispose();
}

using (global::System.Data.SqlClient.SqlConnection connection = new global::System.Data.SqlClient.SqlConnection(""))
{
    using (global::System.Data.SqlClient.SqlCommand command = new global::System.Data.SqlClient.SqlCommand("", connection))
    {
        connection.Open();

        command.ExecuteNonQuery();
    }
}


If I have a simple test method like this:

[TestMethod]
public void SqlTest()
{
  // test code
}


the test passes.

But if I change it to this:

[TestMethod]
public void SqlTest()
{
  MockManager.Init();

  // test code

  MockManager.Verify();
}


I get this exception:

System.Reflection.TargetInvocationException was unhandled
Message="Exception has been thrown by the target of an invocation."
Source="mscorlib"
StackTrace:
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeConstructorInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at TypeMock.MockManager.c(String A_0)
at TypeMock.MockManager.e(String A_0)
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4)
at TypeMock.InternalMockManager.isMocked(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at System.Data.SqlClient.SqlConnection.Dispose(Boolean disposing)
at System.ComponentModel.Component.Finalize()


when exiting the test method.

What am I doing wrong?

It happens with any usage of MockManager.Verify():
  • in the test method
  • in the test cleanup
  • with the VerifyMocks decorator attribute
  • with the ClearMocks decorator attribute
asked by paulo.morgado (11k points)

8 Answers

0 votes
Paulo hi,

it doesnt seem that you are doing anything wrong.
In fact it seem to be working fine here.

We did however notice problems when doing so under the new 2008 beta.
we still need to investigate the difference.

:?: The question is are you working on the new VS 2008 beta? or are you using any part of the new .NET framework?
(If you do it might also be related to other issues you have reported and we failed to reproduce)
answered by lior (13.2k points)
0 votes
What seems to be failing is VSTestHost.exe. I'm getting this message on the Visual Studio Just-In-Time Debugger.

An unhandled exception ('System.Reflection.TargetInvocationException')
occurred in VSTestHost.exe [1172].


Looks like a VS issue but only happens in the conditions I mentioned.

I don't have and never had any version of .NET3.5/VS2008 in this machine.

I'll uninstall TypeMock and resinstall to se how it goes.
answered by paulo.morgado (11k points)
0 votes
No. Still the same.
answered by paulo.morgado (11k points)
0 votes
Paulo hi,
Ok, now I see it happening. It appears to be happening only when you use the MSTest testign framework. I've tried it previously using NUnit.

Anyway I'll keep you posted when we locate the exact problem.
answered by lior (13.2k points)
0 votes
Paulo hi,

We figured out what went wrong.
Its related to the combination of static ctors and field access.
Apparently the SqlConnection is using an inner factory class which due to the internal TypeMock logic does not get initialized properly.
This causes problems in the next time the SqlConnection class is used byby the testing framework in your case.

A quick workaround for this problem is to add the following line at the start of the test:
SqlConnection con = new SqlConnection();

This will initiate the initialization process before the test bypassing the problem.

A full fix will take some time since, it require to first add the ability of mocking fields to TypeMock.
answered by lior (13.2k points)
0 votes
Thanks Lior.

Adding:

[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext)
{
  global::System.Data.SqlClient.SqlConnection connection_mocked = new global::System.Data.SqlClient.SqlConnection("");
}


solved my problem.

But I wonder, shouldn't
MockManager.MockAll<global::System.Data.SqlClient.SqlConnection>(Constructor.NotMocked);
do the same thing?
answered by paulo.morgado (11k points)
0 votes
Hi,

Not exactly. the Idea was to force the static constructor for the SqlConnection class to be called before the recording block.

In any case this is a defect that we will need to fix.
answered by lior (13.2k points)
0 votes
I was unde the impression that the Constructor.NotMocked would take care of it.
answered by paulo.morgado (11k points)
...