简体   繁体   English

单元测试是否会在集成测试之外为DAL提供程序的这个示例添加任何值?

[英]Would a Unit Test add any value to this example of a DAL provider beyond an Integration Test?

public List<int> GetPortfolioList()
{
    using (var connection = new SqlConnection("<connectionString>"))
    using (var command = new SqlCommand("SELECT * FROM Portfolio", connection))
    {
        connection.Open();
        var portfolioTable = SqlHelper.GetDataTable(command);
        var portfolios = from DataRow row
                            in portfolioTable.Rows
                            select int.Parse(row["Portfolio"].ToString());

        return portfolios.ToList();
    }
}

Take this method in a SQL DAL provider to retrieve a list of portfolios, as the name (and code) suggests. 在SQL DAL提供程序中使用此方法来检索投资组合列表,如名称(和代码)所示。 Because the database table for integration testing contains a fairly static set of data we can Assert against several expectations. 因为用于集成测试的数据库表包含一组相当静态的数据,所以我们可以针对多个期望进行断言。 eg The list of portfolios will: - not be empty - contain certain known values - contain no duplicates 例如,投资组合列表将: - 不为空 - 包含某些已知值 - 不包含重复项

Following a peer review, someone insisted that this code isn't being properly tested (in isolation) because it relies on database access. 在进行同行评审之后,有人坚持认为此代码未经过适当测试(孤立地),因为它依赖于数据库访问。 In the case that most of the value is found in ensuring that this method returns data from a database whose state is guaranteed, I've been unable to see the value in mocking away the database call in order to write a unit test for this method. 如果在确保此方法从保证状态的数据库返回数据的过程中找到大部分值,我就无法看到模拟数据库调用的值,以便为此方法编写单元测试。 Am I missing something? 我错过了什么吗?

I'll take the contrary view because I just finished writing a fake db (using in memory lists) to make linq to sql (or linq to anything) unit testable. 我将采取相反的观点,因为我刚刚完成了一个伪db(在内存列表中使用)来使linq到sql(或linq to anything)单元可测试。

This is one question I used to pick a suitable way to fake/mock the database. 这是我用来挑选伪造/模拟数据库的合适方法的一个问题。 (from reading your code though, the embedded "SELECT * FROM" means you are leaning on SQL more than linq, which will make it harder to factor your code into stuff that has to be executed by SQL Server and stuff that linq is capable of dealing with. (从虽然阅读你的代码,嵌入的“SELECT * FROM”意味着您在SQL比LINQ更多,这将使其更难因素代码为能力由SQL Server之类的东西来执行的东西扶着那LINQ能够处理。

How are people unit testing code that uses Linq to SQL 人们如何对使用Linq to SQL的代码进行单元测试

I can now run unit tests that will succeed or fail depending on the suitability of my linq query even if the database is unplugged from the wall . 我现在可以运行单元测试,这些测试将成功或失败,具体取决于我的linq查询的适用性, 即使数据库已从墙上拔下

For example, how is your code to react if row["Portfolio"].ToString() is null, how does the code react when this doesn't return any rows, or returns 2? 例如,如果row [“Portfolio”]。ToString()为null,代码如何响应,当代码没有返回任何行时,代码如何反应,或返回2?

And even if you are only doing integration tests, nunit is not a bad way to integration tests, just be careful not to call them unit tests, lest a purist get upset about it. 即使你只进行集成测试,nunit也不是集成测试的坏方法,只要注意不要将它们称为单元测试,以免纯粹主义者对此感到不安。

The method uses Linq to project some values from the DB into a list of integers - you may want to test that it does that correctly. 该方法使用Linq将数据库中的某些值投影到整数列表中 - 您可能希望测试它是否正确执行。

I would split the code into two - the data retrieval and the projection (Linq query) - you could then test the linq query with mock data , without needing to mock the database. 我会将代码分成两部分 - 数据检索和投影(Linq查询) - 然后您可以使用模拟数据测试linq查询,而无需模拟数据库。

I would also say there is little value in unit testing data access code. 我还要说单元测试数据访问代码没什么价值。

As a testing purist, I believe that I cannot "unit test" a DAL because I cannot do it in isolation. 作为测试纯粹主义者,我相信我不能“单元测试”DAL,因为我不能孤立地做。 This means that a portion of my code, the one interacting with the database, goes without unit testing. 这意味着我的代码的一部分,即与数据库交互的代码,没有单元测试。 The code above looks fine; 上面的代码看起来很好; just make sure that it exists as part of a larger contract that you can test in other objects. 只需确保它作为较大合同的一部分存在,您可以在其他对象中进行测试。

I do perform integration testing, though, by building a database, seeding it, and ensuring that my DAL works. 不过,我通过构建数据库,播种并确保我的DAL正常工作来执行集成测试。

I would agree with you, the only value here will be integration testing, there's really nothing to unit test without being pedantic. 我同意你的观点,这里唯一的价值就是集成测试,没有迂腐,没有什么可以进行单元测试。

The unit test code for any callers of this method should mock this method away, of course. 当然,这种方法的任何调用者的单元测试代码都应该模拟这种方法。

e: The exception cases MathewMartin mentions above would be the only things I would consider worth unit testing in this scenario. e:MathewMartin上面提到的例外情况将是我在这种情况下唯一值得单元测试的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM