gpt4 book ai didi

c# - 使用单元测试和测试数据库

转载 作者:太空狗 更新时间:2023-10-29 22:52:45 26 4
gpt4 key购买 nike

我如何使用 NUnit 和测试数据库来验证我的代码?理论上我会使用模拟(最小起订量),但我的代码更多地处于维护状态并修复它模式,我没有设置所有模拟。

我是否只创建一个测试项目,然后编写实际连接到我的测试数据库的测试并像在应用程序中那样执行代码?然后我用断言检查代码并确保我请求的是我正确返回的内容?

最佳答案

How would I use NUnit and a test database to verify my code? I would in theory use mocks (moq) but my code is more in maintenance shape and fix it mode and I don't have the to setup all the mocks.

仅当您想测试类的确切实现行为时,使用模拟才有用。这意味着您实际上是在断言一个类调用另一个类的特定方法。例如:我想断言 Ninja.Attack() 调用 Sword.Unsheath()。

Do I just create a test project, then write tests that actually connect to my test database and execute the code as I wwould in the app? Then I check the code with asserts and make sure what I'm requesting is what I'm getting back correctly?

这只是一个普通的旧单元测试。如果在实现这一目标方面没有障碍,那么这是一个很好的指标,表明这将是您最有效的测试方法。实用且高效。

您没有提到另一种测试工具,称为 stub 。我强烈建议您阅读这篇经典文章以获取更多信息:

http://martinfowler.com/articles/mocksArentStubs.html

关于c# - 使用单元测试和测试数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13772134/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com