Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

BadImageFormatException with a Property Set

0 votes
Hello,

I'm trying to learn TDD. I 've read all the docs I found out but I can't understand why I've got a problem with my first test !

Thanks for your help.

Error text:
------------
TestCase 'IHM_tests.TestsControleur.Init_test_V1' failed: System.BadImageFormatException: Bad class token.
   at IHM.Controleur.set_ConstruireEstAutorisé(Boolean value)
   [...]\Controleur.vb(9,0): at IHM.Controleur..ctor(IAfficheur unAfficheur) [[ERROR 1]]
   [...]\TestsControleur.vb(31,0): at IHM_tests.TestsControleur.Init_test_V1() [[ERROR 2]] 


Code text (VB.NET in VS2005 with TypeMock 3.0 and TestDriven.NET 2.0)
-----------
<Test> _
Public Sub Init_test_V1()
    MockManager.Init()
    Dim mockIAfficheur As MockObject = MockManager.MockObject(GetType(IHM.IAfficheur))
        mockIAfficheur.ExpectSet("ConstruireEstAutorisé").Args(True)
        Dim monControlleur As IHM.Controleur = New IHM.Controleur(CType(mockIAfficheur.Object, IHM.IAfficheur)) [[ERROR 2]]
        mockIAfficheur.Verify()
    End Sub

Public Interface IAfficheur
    Property ConstruireEstAutorisé() As Boolean
End Interface

Public Class Controleur
    Private monAfficheur As IAfficheur
    Private monConstruireEstAutorisé As Boolean

    Public Sub New(ByVal unAfficheur As IAfficheur)
        monAfficheur = unAfficheur
        ConstruireEstAutorisé = True [[ERROR 1]]
    End Sub

    Property ConstruireEstAutorisé() As Boolean
        Get
            Return monConstruireEstAutorisé
        End Get
        Private Set(ByVal value As Boolean)
            Try
                monAfficheur.ConstruireEstAutorisé = value
            Catch ex As Exception
                Throw ex
            End Try
            monConstruireEstAutorisé = value
        End Set
    End Property
[...] 
asked Jan 30, 2006 by bb (300 points)

20 Answers

0 votes
Hi,
Thanks for the detailed example.
You have found a :twisted: bug, we will fix it and post the fix.
answered Jan 31, 2006 by scott (29,080 points)
0 votes
Hello,

If it helps, I've found a workaround by deleting the Try...Catch !

Public Class Controleur
    [...]

    Property ConstruireEstAutorisé() As Boolean
        [...]
        Private Set(ByVal value As Boolean)
            monAfficheur.ConstruireEstAutorisé = value
            monConstruireEstAutorisé = value
        End Set
    End Property


By the way, thanks a lot for providing such a usefull mock framework !
answered Jan 31, 2006 by bb (300 points)
0 votes
Thanks,
This will help us solve this issue :-)
answered Jan 31, 2006 by scott (29,080 points)
0 votes
Hi,

Are you compiling the test library in Debug or Release mode?
answered Feb 8, 2006 by scott (29,080 points)
0 votes
Hi,

When I first had the problem, I just had changed from Visual Studio 2003 to 2005.

And I can read that when setting up Visual Studio, and choosing "Visual Basic" as primary programming language: "Visual Studio automatically chooses the Debug configuration when you choose Start from the Debug menu and the Release configurations when you use the Build menu".

I have been wondering where to choose build mode for ages and because of your question I have actually search the point. :oops:

Hence I guess I was in Release mode when testing.

Bye.
answered Feb 8, 2006 by bb (300 points)
0 votes
I'm experiencing the same problem!

If the code under test contains a Try...Catch block anywhere the exception BadImageFormatException: Bad class token is thrown.

I'm using:
* TypeMock.NET v3.0.3.0
* Visual Studio 2005, v8.0.50727.42, Trial Edition
* .NET Framework v2.0.50727
* The Team Edition unit testing framework (I've tried with NUnit aswell with the same result)

How do I fix this? Is there a patch or workaround? Removing the Try...Catch's is not an option... :)

/C
answered Mar 10, 2006 by chibbe (220 points)
0 votes
Hi,
I have sent you a fix offline, please tell me if this helps?
answered Mar 10, 2006 by scott (29,080 points)
0 votes
Yes, it was in my todo-list...

With the patch, the test passes. :D
And I checked than my other tests all react just the same as before the patch so everything seems to be absolutely OK with it.

Thanks a lot.
answered Mar 10, 2006 by bb (300 points)
0 votes
Hi Scott,

Thanks for the extremely speedy reply! The weekend got in the way of my testing the patch immediately. Finally back at work, with the patch installed, the BadImageFormatException is no longer thrown! You just saved my life.

However, I now get the System.InvalidProgramException: Common Language Runtime detected an invalid program exception... :(

If I use CInt or CDbl to convert a string to an Integer or Double, respectively, the above mentioned exception is thrown. Using Integer.Parse/Double.Parse to accomplish the same thing works fine.

Repro code:
    
    <TestMethod()> _
    Public Sub FooTest()
        MockManager.Init()

        Dim number1 As Integer = Integer.Parse("5")  ' Ok
        Dim number2 As Integer = CInt("5")  ' Throws exception

        MockManager.Verify()
    End Sub


TypeMock.out contents:
...
Microsoft.VisualStudio.TestTools.Execution.TimeoutMgr instance void Dispose()
T=>0 M=>0
IL_0 = ldarg.0
IL_1 = ldstr 0x7000235B
IL_2 = ldstr 0x70002513
IL_3 = ldnull
IL_4 = call 0x0A000158
IL_5 = brfalse.s 0x2B
IL_6 = ldarg.0
IL_7 = ldstr 0x7000235B
IL_8 = ldstr 0x70002513
IL_9 = ldnull
IL_10 = call 0x0A00015E
IL_11 = dup
IL_12 = ldstr 0x7000235B
IL_13 = ldstr 0x70002513
IL_14 = call 0x0A00015B
IL_15 = isinst 0x010000BC
IL_16 = brtrue.s 0x02
IL_17 = pop
IL_18 = ret
IL_19 = pop
Thin Header - too small (size=34,insersion=62) converting to Fat

Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter instance void Finalize()
T=>0 M=>0
IL_0 = ldarg.0
IL_1 = ldstr 0x700014E0
IL_2 = ldstr 0x70001596
IL_3 = ldnull
IL_4 = call 0x0A0001C1
IL_5 = brfalse.s 0x2B
IL_6 = ldarg.0
IL_7 = ldstr 0x700014E0
IL_8 = ldstr 0x70001596
IL_9 = ldnull
IL_10 = call 0x0A0001C6
IL_11 = dup
IL_12 = ldstr 0x700014E0
IL_13 = ldstr 0x70001596
IL_14 = call 0x0A0001C4
IL_15 = isinst 0x010000C5
IL_16 = brtrue.s 0x02
IL_17 = pop
IL_18 = ret
IL_19 = pop
 nop
 nop
 (Fixed ClassToken)
*INFO: SMALL EH clause TryOffset '0x00000000' HandlerOffset '0x00000050' Changed to: TryOffset '0x00000040' HandlerOffset: '0x00000090'


I guess this is a completely different issue altogether. Perhaps I should have posted in a separate, new thread?

/C
answered Mar 13, 2006 by chibbe (220 points)
0 votes
Hi,
I have sent you a new fix offline, please tell me if this helps?
answered Mar 13, 2006 by scott (29,080 points)
...