如何单元测试代码是公共接口背后非常复杂 [英] How to unit test code that is highly complex behind the public interface

查看:120
本文介绍了如何单元测试代码是公共接口背后非常复杂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我应该如何通过NUnit的是测试这种功能。

I'm wondering how I should be testing this sort of functionality via NUnit.

Public void HighlyComplexCalculationOnAListOfHairyObjects()
{
    // calls 19 private methods totalling ~1000 lines code + comments + whitespace
}

从阅读中,我看到NUnit的目的不是要测试私有有关哪些单元测试应该是哲学原因的方法;但是,试图创建一组测试数据,充分执行所有参与计算的功能几乎不可能。与此同时,计算被分解成若干更小的方法是合理的离散。他们不但是东西,使逻辑意义上相互独立,所以他们都设置为私有完成。

From reading I see that NUnit isn't designed to test private methods for philosophical reasons about what unit testing should be; but trying to create a set of test data that fully executed all the functionality involved in the computation would be nearly impossible. Meanwhile the calculation is broken down into a number of smaller methods that are reasonably discrete. They are not however things that make logical sense to be done independently of each other so they're all set as private.

推荐答案

你混为一谈两件事情。接口(从而暴露出非常少),这种特殊的实现类,从而暴露出了很多。

You've conflated two things. The Interface (which might expose very little) and this particular Implementation class, which might expose a lot more.


  1. 定义最窄可能的接口。

  1. Define the narrowest possible Interface.

与可测试(非私有)的方法和属性的定义实现类。这没关系,如果类有额外的东西

Define the Implementation class with testable (non-private) methods and attributes. It's okay if the class has "extra" stuff.

所有的应用程序应该使用接口,和 - 因此 - 不具备类型安全的访问对类暴露的功能。

All applications should use the Interface, and -- consequently -- don't have type-safe access to the exposed features of the class.

如果有人绕过接口,并直接使用类呢?他们是反社会的 - 你可以放心地忽略它们。因为他们违反使用接口未实施的基本原则,不要为他们提供电话支持。

What if "someone" bypasses the Interface and uses the Class directly? They are sociopaths -- you can safely ignore them. Don't provide them phone support because they violated the fundamental rule of using the Interface not the Implementation.

这篇关于如何单元测试代码是公共接口背后非常复杂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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