gpt4 book ai didi

unit-testing - DAO的单元测试

转载 作者:行者123 更新时间:2023-11-28 20:56:09 24 4
gpt4 key购买 nike

这是我第一次进行单元测试,因此我试图找到有关如何对 DAO 进行单元测试的引用资料。你们可以举一个简单的例子来说明 setUpBeforeClasssetUp如何测试使用此模型在数据库中插入新数据的方法。只是一个使用简单模拟的简单示例。感谢您的考虑

最佳答案

The idea of using mock objects to perform your unit testing strikes me as peculiar as all you are doing is testing your mock objects instead of your real ones. If you think you need to use mock objects to emulate database access then your entire architecture is wrong. I personally build all my software using the 3-Tier Architecture where I can have as many objects as I like in the Business layer, but only 1 object in the Data Access layer. Thus if I wanted to exchange real database access with dummy database access where would I make the change? All 200+ objects in my Business layer, or just the 1 object in the Data Access layer? Why should I then implement a mechanism to change every object within my application when all I really need to do is change one?

Controllers are meant to be integration tested, not unit tested. But the testing pyramid prescribes that the unit level is where the focus should be, so people are sucked into that by default.

Assertions should never fail under any circumstance. If they fail in your tests, it indicates a logical error. Basically, if your function is doing "assert( 0 )" instead of returning an error code, then the function should be re-written.

Can you make massive changes to a class definition — even throw out the whole thing and replace it with a completely different implementation — without impacting any of the code that uses that class's objects?

As has unfortunately happened with a variety of patterns that originate from rigid languages like Java, Dependency Injection has spread and been advocated as a cross-language best practice on trumped up benefits of flexibility and malleability.

引用资料

关于unit-testing - DAO的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17584713/

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