为单个方法编写 classist 和 mockist 单元测试有什么好处吗? [英] Is there any advantage to writing both classist and mockist unit tests for a single method?

查看:70
本文介绍了为单个方法编写 classist 和 mockist 单元测试有什么好处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以按照 http://martinfowler.com/以经典方式或模拟方式编写单元测试文章/mocksArentStubs.html

因为状态和行为都经过测试,所以为单个方法编写 classist 和 mockist 单元测试会增加代码的健壮性吗?

Will writing both classist and mockist unit tests for a single method increase robustness of the code since both state and behaviour is tested?

我的同事似乎一直在嘲笑,因为他们是榜样",所以假设我也会嘲笑,除非我有充分的理由不这样做.(我是单元测试的新手).但是,我觉得只测试 mockist 方式假设未测试私有方法的实现是正确的,这就是为什么我也想要类测试(间接测试私有方法).

My colleagues seem to just be mocking all the way, and as they're the "example", it is assumed that I will be mocking as well unless I have a good reason not to. (I'm new to unit testing). However, I feel that testing just the mockist way assumes the correctness of the implementation of untested private methods, and that's why I want classist tests as well (to test the private methods indirectly).

还是浪费时间?

推荐答案

用 Mocks 测试确实也间接测试私有方法 - 任何私有方法都应该在调用堆栈中具有一些公共方法.如果您的公共方法实现了 100% 的代码覆盖率,那么您的所有私有方法都将被调用.
正如我从 Fowler 文章中回忆的那样,不同之处在于 mockist 测试类的内部工作 - 他们验证您的类是否按预期调用了其他类的 API.如果你不能正确使用它,你的类的功能会受到影响是有道理的 - 例如如果您不向数据库写入一些数据,甚至更糟:写入错误的数据.

Testing with Mocks does test private methods indirectly as well - any private method should have some public method up in the call stack. If you've acheived 100% code coverage of your public methods, all your private methods will get called.
As I recall from Fowler article, the difference is that mockist test the inner working of the class - they verify that your class calls other classes API as they expected to. It makes sense where the functionality of your class will suffer if you'll fail to use it correctely - e.g. if you don't write some data to database, or even worse: write the wrong data.

这篇关于为单个方法编写 classist 和 mockist 单元测试有什么好处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆