Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 537
Spook Avatar asked Nov 14 '13 08:11

Spook


People also ask

How does NSubstitute work?

When we substitute for a class or interface, NSubstitute uses the wonderful Castle DynamicProxy library to generate a new class that inherits from that class or implements that interface. This allows us to use that substitute in place of the original type.

What is NSubstitute?

NSubstitute is a friendly substitute for . NET mocking libraries. It has a simple, succinct syntax to help developers write clearer tests. NSubstitute is designed for Arrange-Act-Assert (AAA) testing and with Test Driven Development (TDD) in mind.


1 Answers

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,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
like image 155
Spook Avatar answered Sep 22 '22 06:09

Spook