简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM