我成功地进行了一段时间的单位测试,但我开始认为,这些测试仅对实际执行相当数量逻辑的班级/方法有用,而教职员工做数学、复杂的业务逻辑,所有优秀的考试候选人都无问题。 我真的要竭力说明如何为另一类物体使用测试:这些物体主要通过代表团操作。
案例:我目前的项目协调了许多数据库和服务。 大多数课程只是收集服务方法,大多数方法具有某种基本条件逻辑,可能是一种旁观,然后是利用其他服务。
like像这样的物体,实际上是唯一可行的检测战略,因此,我为其中几个人精心设计了 mo。 我确实不喜欢它,原因如下:
- Using mocks to specify expectations for behavior makes things break whenever I change the class implementation, even if it s not the sort of change that ought to make a difference to a unit test. To my mind, unit tests ought to test functionality, not specify "the methods needs to do A, then B, then C, and nothing else, in that order." I like tests because I am free to change things with the confidence that I ll know if something breaks - but mocks just make it a pain in the ass to change anything.
- Writing the mocks is often more work than writing the classes themselves, if the intended behavior is simple.
- Because I m using a completely different implementation of all the services and component objects in my test, in the end, all my tests really verify is the most basic skeleton of the behavior: that "if" and "for" statements still work. Boring. I m not worried about those.
The core of my application is really how all the pieces work together, so I m considering ditching unit tests altogether (except for places where they re clearly appropriate) and moving to external integration tests instead - harder to set up, coverage of less possible cases, but actually exercise the system as it is mean to be run.
我看不出任何使用cks子的情况确实有用。
想法?