如何对按日期范围返回文章的查询进行单元测试?

发布于 2024-08-08 16:13:21 字数 141 浏览 5 评论 0原文

假设我有一个文章表,我想为返回所有即将过期的文章的方法编写一个测试。

我将如何为此编写单元测试?如果我模拟该方法返回的文章集合,那它怎么是单元测试呢?

我的意思是我想确保它返回正确的文章数据范围?

有人可以澄清这一点吗?

Say I have a articles table, and I want to write a test for the method that returns all articles that are about to expire.

How would I go about writing a unit test for this? If I mock the collection of articles that the method returns, how is that a unit test?

I mean I want to make sure it returns the correct data range of articles?

Can someone clarify this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笔芯 2024-08-15 16:13:21

如果您正在测试数据访问层本身,您确实可能需要访问一些数据。许多人认为涉及外部依赖项(例如数据库)的测试是“集成”测试而不是“单元”测试——这并不意味着它们不重要。

一种选择是使用轻量级或内存数据库,例如 SQLite。作为设置测试的一部分,使用一组已知数据为数据库播种。然后您可以以受控方式安全地测试结果。

您需要控制的另一个因素:当您创建依赖于当前日期的内容时(如本例所示),您需要一种方法来注入假的“当前日期”以使测试更容易 - 不要只需在数据库中使用“SYSDATE”或等效内容,否则您的测试将变得更加困难(您的测试数据将不得不根据日期进行更改)。

If you're testing the data access layer itself, you will indeed probably need to access some data. Many people consider a test that hits an external dependency such as a database to be an "integration" test rather than a "unit" test -- which doesn't mean they aren't important to do.

One option is to use a lightweight or in-memory database such as SQLite. Seed the database with a set of known data as part of setting up your test. Then you can safely test the results in a controlled way.

Another factor you'll want to control: when you're creating something that depends on the current date (as in this instance) you'll want a way to inject a fake "current date" to make testing easier -- don't just use "SYSDATE" or the equivalent in your database or your testing will be much harder (your test data will have to change depending on the date).

谢绝鈎搭 2024-08-15 16:13:21

我通常有一些由单元测试框架驱动的集成测试,这些测试适用于一组确定的数据(包括为每个测试运行设置数据的脚本,如此处所述)。这样您就可以准确地知道在给定日期范围内应返回多少(以及哪些)文章,然后您还可以为其编写测试。

I typically have some integration tests that are driven by the unit test framework, and that works on a determined set of data (including scripts to set up the data for each test run, as described here). That way you can know exactly how many (and which) articles that should be returned for a given date range, and then you can also author a test for it.

半枫 2024-08-15 16:13:21

如果您模拟它,那么它就是一个单元测试,因为您正在测试尽可能少的功能。

如果你不模拟它,而是针对实时数据库,正如 Dror 所说,这就是一个集成测试。

If you mock it, it's a unit test because you're testing the smallest amount of functionality possible.

If you don't mock it, and go against a live database, as Dror said, it's an integration test.

驱逐舰岛风号 2024-08-15 16:13:21

无法“单元测试”数据访问层,而是可以编写一个“集成测试”来设置一个简单的数据库查询并使用断言来检查结果。

您还需要在测试后运行一个 clean 函数,以确保数据库恢复到其初始状态。

  • 如果您使用 MSTest,您可以使用部署项来设置数据库,否则您将不得不编写简单的帮助程序方法。

如果您想检查业务逻辑 - 即如何处理结果,则使用模拟从查询创建假返回。

There is no way to "unit test" data access layer instead you can write an "integration test" that would setup a simple database query it and use assertions to check the result.

You also need to run a clean function after the test to make sure that the database is restored to it's initial state.

  • if you use MSTest you can use deployment items to setup the database otherwise you'll have to write simple helper method instead.

In case you want to check business logic - i.e.erhow the results are handled then use mocks to create fake retuns from the query.

奢望 2024-08-15 16:13:21

正如前面提到的,这种类型的测试确实是集成测试。您可以使用 MSTest 或 NUnit 作为测试运行程序,但是一个好的经验法则是将这些测试分离到各自的程序集或类别中,这样它们就不会在每次运行快速单元测试时都运行。要完成此集成测试应该不是很困难,您需要执行以下操作:

  1. 创建数据库/清除现有数据库
  2. 使用来自 SQL 脚本或类似 ndbunit,此步骤可以在测试设置或类似的设置例程中完成
  3. 运行集成测试并断言已知上一步中播种的数据与代码返回的结果相匹配
  4. 清理数据库并截断/删除数据库中的记录,以便它们不会对其他测试产生不利影响。

但需要注意的是,如果您没有足够的良好实践来快速轻松地设置系统,那么这些类型的测试可能会非常令人沮丧,因为集成测试非常依赖于整个系统的运行状态。成功通过某些“状态”。使这些类型的测试可重复且易于运行将使未来的集成甚至整个系统范围的测试更容易实施。

This type of test would indeed be an integration test as mentioned previously. You could either use MSTest or NUnit as the test runner, however a good rule of thumb would be to separate these tests out into their own respective assembly or category so they are not ran every time your fast unit tests are ran. To pull off this integration test should not be very difficult, you would would need to do something like the following:

  1. Create the database/clear out an existing database
  2. Seed the database with data from either SQL scripts or a framework like ndbunit, this step can be done in the Test Setup or similar setup routine
  3. Run your integration test and assert that the known data seeded in the previous step matches the results returned from your code
  4. Clean up the database and truncate/delete the records form the database so they do not adversely affect other tests.

Just as a word of caution though is that these types of tests can be very frustrating if you do not have a lot of good practices in place for quick and easy setup of your system since integration tests rely so much on the whole system being at a certain "state" to pass successfully. Making these types of tests repeatable and easy to run will make future integration and even full system wide tests easier to implement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文