测试数据库调用C# [英] Testing database calls C#

查看:147
本文介绍了测试数据库调用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我们在这里讨论术语。我搜索的一切说,单元测试不触及数据库!我不想要一个单元测试。我想要一个测试,当我发送数据到数据库,我知道它正确保存它(和测试其他crud操作)。我有一个存储库层,它基本上接受一个DTO,然后将DTO映射到实体框架模型,然后将该模型保存到数据库。



我需要能够确保向这些方法发送DTO实际上是保存到数据库。



存储库上的示例方法签名是:

  public bool保存(SomeObjectDTO someObject)

我只需要测试这个方法调用是否返回true。



什么是设置测试的最好方法,其中我调用的方法是保存到数据库?



此外,是否有标准方法来设置空白测试数据库?这将是巨大的,如果当我打运行测试它构造一个空数据库,填充它的必要的初始数据,然后执行所有的CRUD操作(所有的我的存储库调用),看看他们都保存像他们应该是



如果这已经回答了我的道歉,但是我搜索的一切都有人说你不应该测试数据库调用,或者人们谈论嘲笑在这里不是非常有用。



我只是想要一个例子和/或关于如何设置这些类型的测试的标准做法。

解决方案

您正在寻找的是集成测试,与编写单元测试同样重要。有很多潜在的错误,你的底层数据提供程序暴露的嘲笑你的存储库不一定会找到(无效的外键,空数据为标记为非空的数据等)。



我认为同样重要的是,您必须针对与生产系统相同的数据库提供商进行测试,否则可能会缺少实施特定的行为。我使用Azure SQL作为项目,而不是创建内存SQL CE实例,我在Azure上有一个单独的数据库,仅用于我的集成测试。



如果你使用XUnit(我确定它存在其他测试框架),有一个方便的属性 [AutoRollback] 将在每次测试运行后自动回滚您的事务。

  [Fact] 
[AutoRollback]
public void AddProductTest_AddsProductAndRetrievesItFromTheDatabase()
{
//连接到你的测试db
YourDbContext dbContext = new YourDbContext(TestConnectionString)

dbContext.Products.Add(new Product(...));

//获取最近添加的产品(或任何您的查询)
var result = dbContext.Single();

//断言正确保存
Assert.Equals(...);
}

测试完成后,您的数据库将再次处于空白状态或任何它是在你运行测试之前)。


First, let's address terminology here. Everything I search for says, "Unit tests don't touch the database!" I don't want a unit test. I want a test that when I send data to a database, I know that it correctly saves it (and testing of other crud operations). I have a repository layer that essentially accepts a DTO, then maps that DTO to an entity framework model, then saves that model to the database.

I need to be able to ensure that sending a DTO to these methods is in fact saving to the database.

An example method signature on the repository is:

public bool Save(SomeObjectDTO someObject)

I just need to test against whether or not this method call returns true.

What is the best way to set up tests where my methods being called are ones that save to the database?

Furthermore, is there a standard way to set up a blank testing database? It would be great if when I hit "Run tests" it constructs an empty database, fills it with initial data that is necessary, and then performs all the CRUD operations (all of my repository calls) to see that they all are saving like they should be.

I apologize if this is already answered, but everything I have searched either has someone saying you shouldn't be testing database calls, or people talking about mocking which is not really useful here.

I just want an example and/or the standard practice on how these types of tests should be set up.

解决方案

What you're looking for is called integration testing, and is just as important as writing unit tests. There's a lot of potential bugs that are exposed by your underlying data provider that mocking your repository won't necessarily find (invalid foreign keys, null data for something marked as not null, etc).

I think it's also important that you test against the same database provider as your production system, otherwise there's a risk of missing implementation specific behavior. I use Azure SQL for a project, and instead of creating an in-memory SQL CE instance, I have a separate database on Azure that's only used for my integration tests.

If you use XUnit (and I'm sure it exists for other testing frameworks), there's a handy attribute [AutoRollback] that will automatically roll back your transaction after each test runs.

[Fact]
[AutoRollback]
public void AddProductTest_AddsProductAndRetrievesItFromTheDatabase()
{
    // connect to your test db
    YourDbContext dbContext = new YourDbContext("TestConnectionString")

    dbContext.Products.Add(new Product(...));

    // get the recently added product (or whatever your query is)
    var result = dbContext.Single();

    // assert everything saved correctly
    Assert.Equals(...);
}

After the test is finished, your database will be at a blank slate again (or whatever it was before you ran the test).

这篇关于测试数据库调用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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