在.Net中存根/模拟数据库

Stubbing / mocking a database in .Net(在.Net中存根/模拟数据库)
本文介绍了在.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 序列化到磁盘.
  • 提取了一个公开所有可用方法的接口.
  • 实现了一个伪数据库类,它只加载序列化数据并返回它.

现在我已经序列化了示例结果,我可以在我的项目中签入,我可以在我的测试中使用假数据库.

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.

更新:

显然所有的答案都没有错,但没有抓住重点.我知道单元测试的基础知识.但是我的代码正在使用 DataTables,所以我不得不以某种方式伪造我的 DataTables.从头开始构建 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 used 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中存根/模拟数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to MOQ an Indexed property(如何最小起订量索引属性)
Mocking generic methods in Moq without specifying T(在 Moq 中模拟泛型方法而不指定 T)
How Moles Isolation framework is implemented?(Moles Isolation 框架是如何实现的?)
Difference between Dependency Injection and Mocking Framework (Ninject vs RhinoMocks or Moq)(依赖注入和模拟框架之间的区别(Ninject vs RhinoMocks 或 Moq))
How to mock Controller.User using moq(如何使用 moq 模拟 Controller.User)
How do I mock a class without an interface?(如何模拟没有接口的类?)