tsohtan
tsohtan

Reputation: 850

How to configure NSubstitute NOT call internal virtual method

I want unit test this class

public class Email 
{ 
    public async virtual Task<bool> Send(  )
    {
        //code
        await Save();
    }

    internal virtual async   Task<bool> Save( )
    {

    } 
}

and this unit test code

var email = Substitute.ForPartsOf<Email>( ); 
email.When(x => x.Save( )).DoNotCallBase(); --> why x.Save will call the real implementation code
email.Save( ).ReturnsForAnyArgs(true);

and is this a correct way to unit test internal method? As i tried changed modifier from internal to public and the unit test is fine.

Please advise.

Thanks

Upvotes: 1

Views: 1120

Answers (1)

David Tchepak
David Tchepak

Reputation: 10484

I think you need to make the internal member visible to DynamicProxyGenAssembly2. This assembly is created by the Castle DynamicProxy library used by NSubstitute (and many other .NET mocking libraries) to create the substitute/mock types.

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] 

If you install the NSubstitute.Analyzers package it should prompt you about this case.

Upvotes: 3

Related Questions