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 ran into a problem attempting to mock an abstract class. The abstract class implements an interface that defines an event. When using Isolate.Fake.Instance on the abstract class, this would cause the unit test to fail with a TypeLoadException with the following description:

Declaration referenced in a method implementation cannot be a final method.

Here is a quick code example that reproduces this issue:

public interface ISomeInterface
{
    event EventHandler SomeEventHandler;
}

public abstract class SomeAbstractClass : ISomeInterface
{
    public event EventHandler SomeEventHandler;
}

[TestClass]
[Isolated]
public class SomeUnitTests
{
    [TestMethod]
    public void SomeTestMethod()
    {
        SomeAbstractClass fakeClass = Isolate.Fake.Instance<SomeAbstractClass>();
    }
}


I found that removing the event from the interface allows the test to pass. The test also passes if the class is not abstract. Since neither of these options is viable in my production project, I found another solution:

public class SomeClass : SomeAbstractClass
{
}

[TestClass]
[Isolated]
public class SomeOtherUnitTests
{
    [TestMethod]
    public void SomeOtherTestMethod()
    {
        SomeAbstractClass fakeClass = Isolate.Fake.Instance<SomeClass>();
    }
}


Creating a concrete implementation of the abstract class for use by the unit tests appears to work. This also means not having to modify the production code just to meet the needs of the unit tests.

I am using version 5.4.5. When I was using 5.3.1, I did not encounter this issue.

Brian
asked by bhunter (3.4k points)

1 Answer

0 votes
We've already found and fixed this issue and it will be available as part of the upcoming version.

I will send you a patch that solves this issue.
answered by dhelper (11.9k points)
...