0

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

tsohtan
  • 830
  • 1
  • 13
  • 33

1 Answers1

2

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.

David Tchepak
  • 9,826
  • 2
  • 56
  • 68
  • https://nsubstitute.github.io/help/how-nsub-works/ says to use this attribute in the test assembly, when what they mean is really to use it in the assembly under test, as you've shown here – Scott Grodberg Oct 10 '20 at 20:56
  • Thanks Scott, I've raised an issue here: https://github.com/nsubstitute/NSubstitute/issues/630 – David Tchepak Oct 11 '20 at 06:31
  • It started to work, when I added this line into the Test project's AssemblyInfo.cs – wodzu Jan 20 '22 at 08:03