c# - 有没有办法在 C# 代码 (Linq-to-sql) 中获取两点之间检索的所有数据

标签 c# asp.net unit-testing linq-to-sql mocking

我的场景如下: 我在一个用 C# Asp.Net 开发的系统中工作(一个巨大的、巨大的、无论如何都会增长的系统)。我正在尝试开始创建一些单元测试以开始重构(相信,它需要重构(有一些 Controller 有 10k、12k 行)。 问题是这个系统中的很多东西都与数据库有关(并且系统与数据库紧密耦合)。数据库上下文在很多代码片段中实例化,而不是注入(inject)。 因此,我现在的观点是将一些数据模拟到本地 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();

关于c# - 有没有办法在 C# 代码 (Linq-to-sql) 中获取两点之间检索的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30260474/

相关文章:

c# - “对象”不包含动态的定义

asp.net - VS2010 图表控件 : Reduce Y-Axis Margin

c - 为什么我们要将单元测试文件与源文件分开?

unit-testing - 使用 Ctest 创建测试组

c# - 使用 LINQ 语法扫描嵌套字典

c# - WPF 将命令绑定(bind)到 Datagrid 内的 Datacontext

c# - 渴望在 NHibernate 中加载一棵树

asp.net - FireFox和AjaxControlToolKit组合框

asp.net - 我应该在 ASP.NET MVC 中构建下一个 Web 应用程序吗?

java - ArchUnit 确保注释存在