What to do when FakeItEasy throws System.BadImageException

Sunday, April 27, 2014

Today I had a weird problem at work. I’ve been working with a new team and they’ve been writing unit tests using FakeItEasy when they got a strange error message:
System.BadImageFormatException: … The signature is incorrect.
Now I’ve been using FakeItEasy for some time and I never once saw this strange behavior.
Googling for the problem only showed a bunch of solved issues but no solution to my specific problem.
Since I needed to solve this problem – I took a closer look at the code in question - the test code looked something like this:
[TestMethod]
public void Test()
{
    var fakeDependency = A.Fake<IMyDependency>();

    var classUnderTest = new MyClass();
    classUnderTest.Run(fakeDependency);

    A.CallTo(() => fakeDependency.SomeMethod()).MustHaveHappened();
}
The exception in question happened on the last line where we’ve used FakeItEasy to verify/assert that a method call occurred. Looking at the problematic interface didn’t help much:

public interface IMyDependency
{
    DataClass SomeMethod();
}
Until they tried changing SomeMethod’s return value to an object which caused the test to run properly.
This strange story made me think that perhaps the problem was the method’s return value (duh) and so I’ve asked in which assembly DataClass was declared. As it turned out it was in another assembly – not the one we were testing (the one with  MyClass inside).

image

And then we understood – since the test didn’t have a reference to AnotherAssembly.dll FakeItEasy had a little problem settting behavior on it or verifying method class on method that returns DataClass .

The fix was simple – add a reference to the assembly with DataClass.

BTW
If that machine had Resharper installed we would have found the problem quicker:

image

Sad but true, in any case once the reference thing was sorted out the test run smoothly.

2 comments

  1. Very interesting catch, Dror.


    I don't know much about this behaviour, but took a quick look to see if more could be learned from the exception and to see if FakeItEasy could (at least) provide a better error message, but I couldn't find anything in the exception to help.

    Actually, the lack of content was remarkable: no stack trace past the test assembly (not even the method):

    System.BadImageFormatException : [C:\Users\blairyat\Documents\Visual Studio 2012\Projects\FakeItEasyQuestions\FakeItEasyQuestions\bin\Debug\FakeItEasyQuestions.dll] The signature is incorrect.

    at FakeItEasyQuestions.Dror.Test()


    And even more interesting (I thought), the exception is thrown as soon as the method is entered, before any FakeItEasy code is called. I think it's actually the framework that's throwing the exception—FakeItEasy doesn't even get a chance to misbehave.

    I'm not trying to take anything away from your post; it's enlightening, and I'm sure will be quite helpful to FakeItEasy clients.

    ReplyDelete
  2. Hi Blair,


    Thanks - I suspected as much, I was hoping there's some trick that would help provide a better error message, it was very frustrating to try and understand this issue.


    At least now there's a solution on the web for other users who encounter this issue.


    Keep on the good work :)

    ReplyDelete

Newer Older
Related Posts Plugin for WordPress, Blogger...