Search code examples
c#internalnsubstitute

Internal properties are not handled by NSubstitute


Suppose, that I've got a following class:

public abstract class Test
{
    internal abstract int Prop 
    {
        get;
    }
}

Now, I try to make a mock using NSubstitute:

var mock = Substitute.For<Test>();

But that fails:

Method 'get_Prop' in type 'Castle.Proxies.TestProxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a621a9e7e5c32e69' does not have an implementation.

I thought of adding NSubstitute to [InternalsVisibleTo], but unfortunately my tested assembly is signed, NSubstitute is not and Internals cannot be VisibleTo unsigned class.

How can I solve this problem?


Solution

  • I've found the solution. One has to add the following line to the Assembly.cs file of the assembly, he wants to test (not the test assembly):

    [assembly:InternalsVisibleTo("DynamicProxyGenAssembly2")]