.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,请更新你的个人资料,而不是将这些信息放在你的问题中。