Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
C# 在构造函数中重构代码还是在存根/填充代码?_C#_Unit Testing_Microsoft Fakes - Fatal编程技术网

C# 在构造函数中重构代码还是在存根/填充代码?

C# 在构造函数中重构代码还是在存根/填充代码?,c#,unit-testing,microsoft-fakes,C#,Unit Testing,Microsoft Fakes,我正在测试一种方法。此方法的构造函数调用其基类构造函数,然后在基类构造函数中设置几个成员,然后执行一个方法从数据库获取数据,然后将一些数据库值设置为更多成员(所有操作都在构造函数中完成) 如果为了测试我的原始方法,在构造函数中删除对数据库的调用并重构它,使其只设置一些成员,那么我可以在测试中轻松地运行构造函数,并删除传入的任何接口(在本例中为1)或者我应该尝试在构造函数中填充进行数据库调用的方法吗 // method under test public override Date

我正在测试一种方法。此方法的构造函数调用其基类构造函数,然后在基类构造函数中设置几个成员,然后执行一个方法从数据库获取数据,然后将一些数据库值设置为更多成员(所有操作都在构造函数中完成)

如果为了测试我的原始方法,在构造函数中删除对数据库的调用并重构它,使其只设置一些成员,那么我可以在测试中轻松地运行构造函数,并删除传入的任何接口(在本例中为1)或者我应该尝试在构造函数中填充进行数据库调用的方法吗

    // method under test
    public override DateTime ResolveDate(ISeries comparisonSeries, DateTime targetDate)
    {
        switch (comparisonSeries.Key)
        {
            case SeriesKey.SomeKey1:
            case SeriesKey.SomeKey2:
            case SeriesKey.SomeKey3:
            case SeriesKey.SomeKey4:
            case SeriesKey.SomeKey5:
                return DateHelper.PreviousOrCurrentQuarterEnd(targetDate);
            default:
                break;
        }

        return base.ResolveDate(comparisonSeries, targetDate);
    }
// constructor
    public ReportForm(SeriesKey key, IAppCache cache)
        : base(key, cache)
    {
        //sets some base members here.
    }

 // base class that the constructor calls
 public abstract class SeriesBase : ISeries
{
    #region variables

    protected IAppCache clientCache;
    // other members below

        // base constructor
public SeriesBase(SeriesKey key, IAppCache cache)
    {
        this.key = key;
        this.clientCache = cache;
        this.infoPack = new SeriesInfo();
        this.InitalizeSeries();
    }
private void InitalizeSeries()
    {
        Dictionary<string, object> qInfoSQL = new Dictionary<string, object>();
        qInfoSQL.Add("@SeriesID", this.key.ToIntString());

        DBServiceResult result = this.clientCache.DBService.FetchFromDB("spSeriesInit", CommandType.StoredProcedure, qInfoSQL);

        if (result.Failed)
            throw new NotImplementedException("whoat");

        this.SetPublishTimeLag(result.XMLResult.Elements("tbl0").FirstOrDefault());
        this.AddFormView(result.XMLResult.Elements("tbl1").FirstOrDefault());
        this.AddPresentation(result.XMLResult.Elements("tbl2").DefaultIfEmpty(null).FirstOrDefault());

        this.SetBridge();
        this.InitializeGenerics();

    }
//正在测试的方法
公共覆盖DateTime ResolveDate(ISeries比较系列,DateTime targetDate)
{
开关(comparisonSeries.Key)
{
案例系列Key.SomeKey1:
case SeriesKey.SomeKey2:
案例系列key.SomeKey3:
案例系列Key.SomeKey4:
案例系列key.SomeKey5:
返回日期helper.PreviousOrCurrentQuarterEnd(targetDate);
违约:
打破
}
返回base.ResolveDate(比较系列,targetDate);
}
//建造师
公共报告表单(SeriesKey、IAppCache缓存)
:base(键、缓存)
{
//在此设置一些基本成员。
}
//构造函数调用的基类
公共抽象类系列库:ISeries
{
#区域变量
受保护的IAppCache客户端缓存;
//以下其他成员
//基本构造函数
公共序列库(序列密钥、IAppCache缓存)
{
this.key=key;
this.clientCache=cache;
this.infoPack=new serieInfo();
this.initializeseries();
}
私有void initializeseries()
{
Dictionary qInfoSQL=新字典();
添加(“@SeriesID”,this.key.ToIntString());
DBServiceResult result=this.clientCache.DBService.FetchFromDB(“spSeriesInit”,CommandType.StoredProcess,qInfoSQL);
if(result.Failed)
抛出新的NotImplementedException(“whoat”);
SetPublishTimeLag(result.XMLResult.Elements(“tbl0”).FirstOrDefault());
this.AddFormView(result.XMLResult.Elements(“tbl1”).FirstOrDefault();
此.AddPresentation(result.XMLResult.Elements(“tbl2”).DefaultIfEmpty(null).FirstOrDefault());
这个。SetBridge();
这个。初始化enerics();
}
此.clientCache.DBService.FetchFromDB(…)方法正在调用另一个接口DBService,该接口DBService调用方法FetchFromDB,此FetchFromDB方法将转到QSL服务器并检索数据集

这是我的测试方法

public void TestResolveDate()
    {
        //using (ShimsContext.Create())
        //{
        //    Stat.Pi.Data.Fakes.ShimAuthenticator
        //}

        var appCache = new Fakes.StubIAppCache();
        appCache.DBServiceGet = DbServiceGet;


        ReportFORM formReport = new ReportForm(SeriesKey.SomeKey1, appCache);

        var series = new Fr Fakes.StubISeries();

        DateTime date = formReport.ResolveDate(series, DateTime.Now);


        //Assert.
    }
private IDBService DbServiceGet()
    {
        Dictionary<string, object> dict = new Dictionary<string, object>();
        dict.Add("@SeriesID", "50");

        var service = new Fakes.StubIDBService();
        service.FetchFromUnicornStringCommandTypeDictionaryOfStringObject = (s, type, arg3) => dict 
        return service;

    }
public void TestResolveDate()
{
//使用(ShimsContext.Create())
//{
//Stat.Pi.Data.Fakes.simauthenticator
//}
var appCache=new Fakes.StubIAppCache();
appCache.DBServiceGet=DBServiceGet;
ReportFormReport=新的ReportFORM(SeriesKey.SomeKey1,appCache);
var series=新Fr Fakes.StubISeries();
DateTime日期=formReport.ResolveDate(系列,DateTime.Now);
//断言。
}
私有IDBService DbServiceGet()
{
Dictionary dict=新字典();
添加(“SeriesID”,“50”);
var service=new Fakes.StubIDBService();
service.FetchFromUnicornStringCommandTypeDictionaryOfStringObject=(s,type,arg3)=>dict
回程服务;
}
1)正如Henk Holterman所说,构造函数不应该调用数据库。这既增加了耦合,又降低了内聚性

2) 您可能正在寻找的设计模式称为工厂模式——这将构造、初始化和填充分离开来。另一方面,您可能希望引入一个帮助器或处理程序类来处理对象的持久性方面


3) 放入数据库访问存根以执行测试是合理的,因为重构代码可能超出您的权限。但是,就我个人而言,我建议引入一个持久性帮助器类来将新创建的对象绑定到数据库值。

您能展示源代码吗?这对任何讨论都会有帮助。展示给我们请给我一些代码…我认为描述已经足够清楚了,没有代码。从(摘要)调用Db基cTor看起来与我非常紧密的耦合。我会考虑一些IOC。你能建议我用FAKE来截取数据访问接口DbService和它调用FETCHORACDB的方法吗?我试着把这个。{Series.Fakes.ShimSeriesBase.AllInstances.initializeseries=()=>{};}但是编译器说“错误17委托'Microsoft.QualityTools.Testing.Fakes.FakesDelegates.Action'不接受0个参数”