打桩/嘲讽在.net数据库 [英] Stubbing / mocking a database in .Net

查看:127
本文介绍了打桩/嘲讽在.net数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务,基本上只是执行一些存储过程,转换数据并将其发送到浏览器。没有花哨的ORM映射器或类似的东西参与。为了能够编写测试,而无需访问数据库,我也做了以下内容:

I have a webservice which basically just executes some stored procedures, transforms the data and sends it to the browser. No fancy ORM mapper or something like that involved. To be able to write test without accessing the database, I have done the following:


  • 我已提取到数据库的所有调用为一类。该方法只返回DataSet和DataTable对象。

  • 执行的示例调用每个方法和序列化的DataSet / DataTable的磁盘。

  • 中提取的界面展示所有可用的方法。

  • 实施了刚刚加载序列化的数据并返回一个假的数据库类。

  • I have extracted all calls to the DB into one class. The methods return just the DataSet and DataTable objects.
  • Executed a sample call for each method and serialized the DataSet/DataTable to disk.
  • Extracted an interface exposing all available methods.
  • Implemented a fake database class which just loads the serialized data and returns it.

现在我已经系列化样本的结果,我可以用我的项目检查,我可以用在我的测试假货数据库。

Now I have serialized sample results which I can check in with my project, and I can use the fake database in my tests.

这工作得很好为了我。有一些框架,使创建和装载样本数据更容易? 。我当前的项目虽小,但我会用较大的项目相同的架构

This works quite well for me. Is there some framework which makes creating and loading the sample data easier? My current project is small, but I would use the same schema in larger projects.

更新:

显然,所有的答案都没有错,但不得要领。我知道单元测试的基础知识。但是,我的代码正在与数据表,所以我会以某种方式伪造我的数据表。从头开始构建一个DataTable不是一件容易的事,它会膨胀我的测试,减少可读性。就我而言,这将是完全不可能手动生成有用的样本数据。

Obviously all answers are not wrong, but miss the point. I'm aware of the basics of unit testing. But my code is working with DataTables, so I would have to somehow fake my DataTables. Building a DataTable from scratch is not an easy task, and it would bloat my tests and reduce readability. In my case, it would be quite impossible to generate useful sample data by hand.

因此​​,我对执行一个样本数据库一些示例调用得到一些数据表。我已经连载这些表到磁盘,然后使用序列化版本测试时创建我的假数据表。这样的测试是独立的数据库。

Therefore, I executed some sample calls against a sample database to get some DataTables. I have serialized these tables to disk and use the serialized versions to create my fake DataTables when testing. That way the tests are independent of the database.

有关于如何构造代码,以使表的序列化更容易的不同选项。但那些不在这一点上需要讨论的实施细则。我的问题是这样的:

There are different options regarding how to structure the code, to make deserialization of the tables easier. But those are implementation details which don't need a discussion at this point. My problem is the following:

管理样本通话和(反)序列表是单调乏味的工作。我一直在寻找一些工具来简化这一过程。

Managing the sample calls and (de)serializing the tables is tedious work. I was looking for some tools to make this easier.

推荐答案

从阅读其他的答案和您所做的各种评论,它似乎要生成集成测试,不打大型数据库的数据集人口更简单的方法。

From reading the other answers and various comments you've made, it seems you want an easier way to generate large populated datasets for integration testing that doesn't hit the database.

NBuilder 是,我已经成功地创造了大量有很大的开放源码库的试验数据。简单地结合NBuilder,一些基本的POCO对象类,以及一些反思 - 您有足够庞大的数据表,你可以很容易地整合到数据集在任何时间:

NBuilder is a great open-source library that I've successfully to create large amounts of test data. Simply combine NBuilder, a few basic POCO object classes, and some reflection - you'll have plenty of huge datatables you can easily combine into datasets in no time:

public class Person
{
    public string First { get; set; }
    public string Last { get; set; }
    public DateTime Birthday { get; set; }
}

private DataTable GenerateDataTable<T>(int rows)
{
    var datatable = new DataTable(typeof(T).Name);
    typeof(T).GetProperties().ToList().ForEach(
        x => datatable.Columns.Add(x.Name));
    Builder<T>.CreateListOfSize(rows).Build()
        .ToList().ForEach(
            x => datatable.LoadDataRow(x.GetType().GetProperties().Select(
                y => y.GetValue(x, null)).ToArray(), true));
    return datatable;
}

var dataset = new DataSet();
dataset.Tables.AddRange(new[]{
        GenerateDataTable<Person>(50),
        GenerateDataTable<Dog>(100)});

这篇关于打桩/嘲讽在.net数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆