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
0 votes
I have a problem with a VS2005 unit test that uses TypeMock 3.0.3.

When I run the test by using Debug Test (even if there are no breakpoints) the test will complete successfully.

Running the test normally results in an exception being thrown:
"Test method Tests.ConfigurationSettings.SettingsTest.TestGetConnectionString threw exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.BadImageFormatException: Bad class token.."

Even weirder, this test used to run correctly!
asked by thepuffin (2.4k points)

9 Answers

0 votes
Hi,
How are you running the tests?
What language are you writting in?
Do you know what method is bad? can you send the method?
Can you send the TypeMock.out log of the method?
answered by richard (3.9k points)
0 votes
Hi Richard.

I actually managed to work around the problem by going through line-by-line and commenting stuff out until it worked.

I have a method called in the constructor that I am mocking using TypeMock with the following code:

Mock settingsMock = MockManager.Mock(typeof(Settings), Constructor.NotMocked);
settingsMock.ExpectCall("GetApplicationSettings");


The method looked like this before, with the line in bold causing the error:

private void GetApplicationSettings()
{
NameValueCollection localSection = ConfigurationManager.GetSection("ApplicationConfiguration") as NameValueCollection;
if (localSection == null)
throw new Exception("You haven't provided the correct configuration sections in your app.config, you naughty person!");

_configDbConnectionString = localSection["ConfigurationDatabase"];

if (localSection["ApplicationName"] != null)
_applicationName = localSection["ApplicationName"];

try
{
if (localSection["Mode"] != null)
_mode = (ApplicationMode)Enum.Parse(typeof(ApplicationMode), localSection["Mode"]);
}
catch
{ }

string logLever = localSection["LogLevel"];
if (loglevel == null)
_minimumLogSeverity = LogSeverity.Error;
else
_minimumLogSeverity = (LogSeverity)Enum.Parse(typeof(LogSeverity), loglevel);

if (_configDbConnectionString == null)
throw new ApplicationException("Unable to retrieve location of Configuration Database from app.config or web.config file.");
}

Changing that block of code to this
if (localSection["LogLevel"] == null)
_minimumLogSeverity = LogSeverity.Error;
else
_minimumLogSeverity = (LogSeverity)Enum.Parse(typeof(LogSeverity), localSection["LogLevel"]);

fixes the problem!

What gives? Why does TypeMock care what's in the method that I'm mocking?
answered by thepuffin (2.4k points)
0 votes
Please send the typemock log for the GetApplicationSettings and a few methods after.
answered by richard (3.9k points)
0 votes
Sure, except I don't know where the TypeMock log is. :)
answered by thepuffin (2.4k points)
0 votes
answered by richard (3.9k points)
0 votes
Okay, this is the version that worked:

========================================
DotNetSolutions.Framework.ConfigurationSettings.Settings instance void GetApplicationSettings()
T=>0 M=>0
IL_0 = ldarg.0
IL_1 = ldstr 0x700032F0
IL_2 = ldstr 0x7000341B
IL_3 = ldnull
IL_4 = call 0x0A000302
IL_5 = brfalse.s 0x2B
IL_6 = ldarg.0
IL_7 = ldstr 0x700032F0
IL_8 = ldstr 0x7000341B
IL_9 = ldnull
IL_10 = call 0x0A000306
IL_11 = dup
IL_12 = ldstr 0x700032F0
IL_13 = ldstr 0x7000341B
IL_14 = call 0x0A000305
IL_15 = isinst 0x01000122
IL_16 = brtrue.s 0x02
IL_17 = pop
IL_18 = ret
IL_19 = pop
nop
nop
*INFO: SMALL EH clause TryOffset '0x0000010B' HandlerOffset '0x000001CB' Changed to: TryOffset '0x0000014B' HandlerOffset: '0x0000020B'

aa instance bool a(class System.Object)
ak instance class a b(class T)
ak instance bool c()
ak instance bool g()
ak instance class c j()
ak instance class c a(int32)
c instance bool j()
ak instance void b()
y instance class System.Object get_Item(int32)
c instance bool k()
y instance int32 a()
c instance bool d()
ak instance bool d()
ao instance class System.Object a(class System.String,class System.Object[],class System.Object,class System.Object,bool&)
ak instance class c a(class System.Object[],class S,class System.String)
ak instance void e()
c instance void a()
ak instance bool b(class System.Object[],class S,class System.String)
c instance bool g()
c instance bool c()
c instance class System.Object b()
c instance int32 n()
c instance bool h()
ak instance int32 a(bool)
ak instance int32 a(class c,bool)
c instance value class T i()
c instance bool e()
c instance int32 l()
ao instance void a(class System.Object,class System.String,class System.Object[],class System.Object,bool)
ak instance void a(class System.Object,class S,class System.String,class System.Object[],class System.Object,bool,bool)
e instance void .ctor(class System.String)
at instance void .ctor(class System.Object[],bool[])

<continues with other methods on the object>
====================================

This is the broken one:
====================================
GetApplicationSettings()
T=>0 M=>0
IL_0 = ldarg.0
IL_1 = ldstr 0x700032F0
IL_2 = ldstr 0x7000341B
IL_3 = ldnull
IL_4 = call 0x0A000302
IL_5 = brfalse.s 0x2B
IL_6 = ldarg.0
IL_7 = ldstr 0x700032F0
IL_8 = ldstr 0x7000341B
IL_9 = ldnull
IL_10 = call 0x0A000306
IL_11 = dup
IL_12 = ldstr 0x700032F0
IL_13 = ldstr 0x7000341B
IL_14 = call 0x0A000305
IL_15 = isinst 0x01000122
IL_16 = brtrue.s 0x02
IL_17 = pop
IL_18 = ret
IL_19 = pop
nop
nop
*INFO: SMALL EH clause TryOffset '0x0000010B' HandlerOffset '0x000001CB' Changed to: TryOffset '0x0000014B' HandlerOffset: '0x0000020B'

PreCompiled:System.Environment void InitResourceHelper()
PreCompiled:System.Environment+ResourceHelper instance void GetResourceStringCode(class System.Object)
Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter instance void HandleMethodException(class S)
T=>0 M=>0
IL_0 = ldarg.0
IL_1 = ldstr 0x700014E0
IL_2 = ldstr 0x7000159D
IL_3 = ldnull
IL_4 = call 0x0A0001C1
IL_5 = brfalse.s 0x3E
IL_6 = ldarg.0
IL_7 = ldstr 0x700014E0
IL_8 = ldstr 0x7000159D
IL_9 = ldnull
IL_10 = ldarg.1
IL_11 = call 0x0A0001C7
IL_12 = dup
IL_13 = ldstr 0x700014E0
IL_14 = ldstr 0x7000159D
IL_15 = ldc.i4.0
IL_16 = call 0x0A0001C3
IL_17 = starg.s 0x01
IL_18 = ldstr 0x700014E0
IL_19 = ldstr 0x7000159D
IL_20 = call 0x0A0001C4
IL_21 = isinst 0x010000C5
IL_22 = brtrue.s 0x02
IL_23 = pop
IL_24 = ret
IL_25 = pop
nop
nop
nop

Microsoft.VisualStudio.TestTools.Common.TestResult instance void set_Outcome(value class M)
T=>0 M=>0
IL_0 = ldarg.0
IL_1 = ldstr 0x70005A90
IL_2 = ldstr 0x70005AF6
IL_3 = ldnull
IL_4 = call 0x0A0003AB
IL_5 = brfalse.s 0x49
IL_6 = ldarg.0
IL_7 = ldstr 0x70005A90
IL_8 = ldstr 0x70005AF6
IL_9 = ldnull
IL_10 = ldarg.1
IL_11 = box 0x02000014
IL_12 = call 0x0A0003AF
IL_13 = dup
IL_14 = ldstr 0x70005A90
IL_15 = ldstr 0x70005AF6
IL_16 = ldc.i4.0
IL_17 = call 0x0A0003AD
IL_18 = unbox 0x02000014
IL_19 = ldind.i4
IL_20 = starg.s 0x01
IL_21 = ldstr 0x70005A90
IL_22 = ldstr 0x70005AF6
IL_23 = call 0x0A0003AE
IL_24 = isinst 0x01000118
IL_25 = brtrue.s 0x02
IL_26 = pop
IL_27 = ret
IL_28 = pop
Thin Header - too small (size=9,insersion=92) converting to Fat

Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestExecuter instance void HandleCodeCoverageSigningException(class S)
T=>0 M=>0
IL_0 = ldarg.0
IL_1 = ldstr 0x700014E0
IL_2 = ldstr 0x700015C9
IL_3 = ldnull
IL_4 = call 0x0A0001C1
IL_5 = brfalse.s 0x3E
IL_6 = ldarg.0
IL_7 = ldstr 0x700014E0
IL_8 = ldstr 0x700015C9
IL_9 = ldnull
IL_10 = ldarg.1
IL_11 = call 0x0A0001C7
IL_12 = dup
IL_13 = ldstr 0x700014E0
IL_14 = ldstr 0x700015C9
IL_15 = ldc.i4.0
IL_16 = call 0x0A0001C3
IL_17 = starg.s 0x01
IL_18 = ldstr 0x700014E0
IL_19 = ldstr 0x700015C9
IL_20 = call 0x0A0001C4
IL_21 = isinst 0x010000C5
IL_22 = brtrue.s 0x02
IL_23 = pop
IL_24 = ret
IL_25 = pop
nop
nop
nop

Microsoft.VisualStudio.TestTools.TestTypes.Unit.TestMethod instance class System.String get_Name()
T=>0 M=>0
IL_0 = ldarg.0
IL_1 = ldstr 0x70000222
IL_2 = ldstr 0x70000298
IL_3 = ldnull
IL_4 = call 0x0A000066
IL_5 = brfalse.s 0x2A
IL_6 = ldarg.0
IL_7 = ldstr 0x70000222
IL_8 = ldstr 0x70000298
IL_9 = ldnull
IL_10 = call 0x0A00006A
IL_11 = dup
IL_12 = ldstr 0x70000222
IL_13 = ldstr 0x70000298
IL_14 = call 0x0A000069
IL_15 = isinst 0x01000058
IL_16 = brtrue.s 0x01
IL_17 = ret
IL_18 = pop
Thin Header - too small (size=8,insersion=61) converting to Fat

Microsoft.VisualStudio.TestTools.TestTypes.Unit.UTAHelper class System.String GetExceptionMsg(class S)
T=>0 M=>0
IL_0 = ldnull
IL_1 = ldstr 0x7000160F
IL_2 = ldstr 0x70001683
IL_3 = ldnull
IL_4 = call 0x0A0001C1
IL_5 = brfalse.s 0x3D
IL_6 = ldnull
IL_7 = ldstr 0x7000160F
IL_8 = ldstr 0x70001683
IL_9 = ldnull
IL_10 = ldarg.0
IL_11 = call 0x0A0001C7
IL_12 = dup
IL_13 = ldstr 0x7000160F
IL_14 = ldstr 0x70001683
IL_15 = ldc.i4.0
IL_16 = call 0x0A0001C3
IL_17 = starg.s 0x00
IL_18 = ldstr 0x7000160F
IL_19 = ldstr 0x70001683
IL_20 = call 0x0A0001C4
IL_21 = isinst 0x010000C5
IL_22 = brtrue.s 0x01
IL_23 = ret
IL_24 = pop

<continues with exception handling stuff>
answered by thepuffin (2.4k points)
0 votes
I have sent you a fix offline. 8)
answered by richard (3.9k points)
0 votes
Richard, I can confirm that your fix worked for my problem.

Yet again I'm impressed with your speedy response to reported bugs.

Thanks!
answered by thepuffin (2.4k points)
0 votes
Fixed in version 3.1
answered by scott (32k points)
...