我的场景如下:我正在一个用C#Asp.Net开发的系统中工作(无论如何,这是一个庞大,庞大且定义明确的系统)。而且我正在尝试开始创建一些单元测试以开始重构(相信,这是需要重构的(有些控制器具有10k,12k线)。问题是该系统中的很多东西都与数据库有关(并且系统是数据库上下文是通过很多代码实例化的,而不是注入的。因此,我现在的观点是将一些数据模拟到本地MDB文件中,以重构代码并创建具有自己的MDB的单元测试(具有所有数据库结构,但仅包含他将使用的数据)。
在我什么呢?像这样:
[TestMethod()]
public void AnyTest()
{
var dbLogger = new DbLogger(); //this class is not created, it's just an example.
dbLogger.Start();
WorstWrittenMethodEver(); //this method will call any other methods insides,
//a couple of then
//and I really don't know the order and the
//complexity (much times very high), and this will probably
//instantiate the DataContext a lot of times and do
//a lot of data retrieval.
db.StopLog();
Console.WriteLine(db.DataRetrieved); //And in this line I will print all the tables
//and data retrieved between this two points.
}
之后,我将获得该数据,将其模拟到一个MDB文件中,并重构上面的单元测试以获得真正的单元测试。
反正有这样做吗?
看起来这不是一件容易的事。而且我认为您应该使用一些流行且经过测试的库或工具。我的建议是使用MiniProfiler。它允许捕获所有SQL查询(还包括对LinqToSql的支持)。它具有良好的UI和API,可在您的代码中进行交互。实际上,要获取所有SQL查询数据,可以使用以下方法:
MiniProfiler.Current.GetSqlTimings();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句