Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 存根访问受保护的变量_.net_Unit Testing_Protected_Moles_Stubs - Fatal编程技术网

.net 存根访问受保护的变量

.net 存根访问受保护的变量,.net,unit-testing,protected,moles,stubs,.net,Unit Testing,Protected,Moles,Stubs,我需要测试这个类: public abstract class GaBase { protected GoogleAnalyticsInfo GAInfo; protected abstract void PopulateGAInfo(); public string GetGoogleAnalyticsTag() { //Return any info related to GAInfo } //Some other stu

我需要测试这个类:

public abstract class GaBase
{
    protected GoogleAnalyticsInfo GAInfo;

    protected abstract void PopulateGAInfo();

    public string GetGoogleAnalyticsTag()
    {
        //Return any info related to GAInfo
    }

    //Some other stuffs
}
我需要对
GetGoogleAnalyticsTag
方法进行单元测试,但我需要设置属性
GAInfo
,以正确测试它。在生产代码中,当我们从这个类派生时,我们使用我的
PopulateGaInfo
方法来实现这一点

如何使用存根设置
GAInfo

以下是我的测试方法:

public void MyTest1()
{          
    var ga =  new StubGaBase()
        {
            PopulateGAInfo01 = () =>
                {
                    // How can I set GAInfo here?
                }
        };

    // The method I need to test
    var script = ga.GetGoogleAnalyticsTag();

    // My asserts
}

有一种称为“要测试的子类”的模式,它建议如果您需要访问方法或行为(例如模拟引发事件),您可以手动滚动一个存根/模拟,以公开测试所需的功能。此存根仅存在于测试项目中


如果您只对测试抽象类的受保护变量感兴趣,只需创建一个派生类,并公开一个执行该工作并返回您感兴趣的值的方法。

awnser取决于您想要测试的内容和GetGoogleAnalyticsTag()方法的工作。如果没有实施,很难回答你的问题。如果你想被称为/称呼为Benjamin,请更新你的个人资料,而不是将这些信息放在你的问题中。