数据访问层的单元测试 - 测试更新方法?

发布于 2024-07-19 06:30:52 字数 181 浏览 6 评论 0原文

我正在考虑为数据访问层中的某些类添加一些单元测试,并且正在查看没有返回值的更新例程。 它只是根据您在提供的任何列名处提供的 id 更新行。

在此方法内部,我们收集参数并将它们传递给辅助例程,该例程调用存储过程来更新表。

在这种情况下,是否有推荐的方法来进行单元测试? 我很难想到不依赖其他方法的测试。

I'm looking into adding some unit tests for some classes in my data access layer and I'm looking at an update routine that has no return value. It simply updates a row based on the id you provide at whichever column name you provide.

Inside of this method, we collect the parameters and pass them to a helper routine which calls the stored procedure to update the table.

Is there a recommended approach for how to do unit testing in such a scenario? I'm having a hard time thinking of a test that wouldn't depend on other methods.

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

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

发布评论

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

评论(4

冷默言语 2024-07-26 06:30:52

首先测试从数据库读取数据的方法。

然后您可以调用更新函数,并使用上面测试的函数来验证更新的值是否正确。

我倾向于在单元测试中使用其他方法,只要我有测试也测试那些被调用的方法。

如果您的辅助函数位于数据库(存储过程或函数)中,那么只需首先使用 DatabaseUnitTest 测试它们,然后测试 Visual Basic 代码。

Test the method that reads the data from the database, first.

Then you can call the update function, and use the function that was tested above, to verify that the value that was updated is correct.

I tend to use other methods in my unit tests as long as I have tests that also test those that were called.

If your helper functions are in the database (stored procedures or functions) then just test those with a DatabaseUnitTest first, then test the visual basic code.

剧终人散尽 2024-07-26 06:30:52

我只会使用查找方法来验证数据是否已正确更新。

是的,从技术上讲,这将依赖于查找方法的正常工作,但我认为您不一定必须避免这种依赖性。 只需确保查找方法也经过测试即可。

I would just use a lookup method to validate that the data was properly updated.

Yes, technically this would relay on the lookup method working properly, but I don't think you necessarily have to avoid that dependency. Just make sure the lookup method is tested as well.

柒七 2024-07-26 06:30:52

我将使用该方法来获取该数据并检查您更新的内容的返回值并断言预期值。 这确实假设用于检索数据的方法已经过测试并且工作正常。

I would use the method to get that data and check the return value to what you updated and Assert the expected value. This does assume the method used to retrieve the data has been tested and works correctly.

意中人 2024-07-26 06:30:52

我使用 nhibernate 和事务,对于单元测试,我不提交到数据库,但我刷新会话,如果需要,它会给出相同的错误,但不会写入数据。

当然,如果您有构建服务器,您只需针对每个构建时新创建的新数据库运行单元测试。 尝试使用基于文件的数据库,例如 firebird 等。

I use nhibernate and transactions and for unittests I don't commit to the database but I flush the session which gives the same errors if needed but doesn't write the data.

Of course if you have a build server you just run the unittests against a freshly made database which is freshly made on each build. Try using an filebased database like firebird or something.

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