RSS
Facebook
Twitter

Sunday, April 27, 2014

What to do when FakeItEasy throws System.BadImageException

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:

Post a Comment

Related Posts Plugin for WordPress, Blogger...