单元测试辅助方法? [英] Unit test helper methods?

查看:200
本文介绍了单元测试辅助方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以前有大量方法的类,所以我把这个方法的工作细分为'helper'方法。

I have classes which previously had massive methods so I subdivided the work of this method into 'helper' methods.

这些辅助方法被声明为 private 以强制执行封装 - 但是我想对大型公共方法进行单元测试。对辅助方法进行单元测试也是好的,就好像其中一个方法失败了调用它的公共方法也会失败,这样我们就可以找出它失败的原因了吗?

These helper methods are declared private to enforce encapsulation - however I want to unit test the big public methods. Is it good to unit test the helper methods too as if one of them fail the public method that calls it will also fail, and this way we can identify why it failed?

另外,为了使用模拟对象测试这些,我需要将其可见性从私有更改为受保护,这是否可取?

Also in order to test these using a mock object I would need to change their visibility from private to protected, is this desirable?

推荐答案

一种方法是省略 private 并将测试放在同一个包中。然后测试可以调用内部方法,但没有其他人(在包外面)可以。

One way is to omit private and put the tests in the same package. Then the tests can call the internal methods but no one else (= outside the package) can.

此外,失败的内部方法应该产生错误消息,这使得它很容易修复问题。当您将代码投入生产时,您将看到比测试更少的内容,并且您将面临很大的压力来快速解决问题。因此,在这里度过一分钟,一小时后你的老板会坐在你的脖子上。

Also, failing internal methods should produce error messages which make it easy to fix the issue. When you put the code into production, you'll see less than the tests and you'll be under a lot of pressure to fix the issues fast. So one minute spent here will save you one hour later with your boss sitting in your neck.

这篇关于单元测试辅助方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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