简体   繁体   中英

How to write nunit test case for internal type class having protected methods?

How to write Nunit test case for a class of internal type having protected methods. Should i use inheritance for this? Following is the class, for which i am trying to write test case:-

internal class CSGetBuyerAbuseReportsRequestTanslator
{
    protected override CSGetBuyerAbuseReportsRequestType BusinessToService(IEntityTranslatorService service, BuyerAbuseRequest value)
    {
        //code
    }
 }

Please, suggest me how to write test case for this class?

I'm assuming you have a separate assembly that contains unit tests. If so, you can add a line to the AssemblyInfo.cs file of the assembly being tested similar to the following (substitute your unit test assembly's name for AssemblyB ):

[assembly: InternalsVisibleTo("AssemblyB")]

This will allow your unit test assembly to access the internal class(es) contained in the assembly you are testing. See this MSDN post , which states the following regarding the use of InternalsVisibleTo :

This is especially convenient...when test code runs in a separate assembly but requires access to members in the assembly being tested that are marked as Friend (Visual Basic) or internal (C#).

You should use Test-Specific subclass if you want to test protected methods explicitely.

The simpliest example is below:

internal class CSGetBuyerAbuseReportsRequestTanslatorTSS : CSGetBuyerAbuseReportsRequestTanslator
{
    public CSGetBuyerAbuseReportsRequestType ExposeBusinessToService(IEntityTranslatorService service, BuyerAbuseRequest value) 
    {
        return BusinessToServic(service, value);
    }
}

And in you tests you will be able to call protected methods through the public ones in a Test-Specific subclass.

[TestFixture]
public class SubclassTests
{
    [Test]
    public void Test()
    {
        var sut = new CSGetBuyerAbuseReportsRequestTanslatorTSS();

        //arrange your system under test here
        //...

        var result = sut.ExposeBusinessToService(service, value);
        Assert.AreEqual(expectedResult, result);
    }
}

The other option is to use reflection:

[TestFixture]
public class ReflectionTests
{
    [Test]
    public void Test()
    {
        var sut = new CSGetBuyerAbuseReportsRequestTanslator();

        //arrange your system under test here
        //...

        var result = sut.GetType()
                        .GetMethod("BusinessToService", BindingFlags.NonPublic)
                        .Invoke(result, new [] {service, value});

        Assert.AreEqual(expectedResult, result);
    }
}

However you should consider splitting your class if you can't test through its public interfaces.

Your class is internal in the example, if you have tests in a separate assembly you should use InternalsVisibleToAttribute to be able to access it from the tests assembly.

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