只在中等规模的班级上测试公共方法? [英] Testing only the public method on a mid sized class?

查看:27
本文介绍了只在中等规模的班级上测试公共方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 FooJob() 的类,它在 WCF Windows 服务上运行.这个类只有 2 个公共方法,构造函数和一个 Run() 方法.

I have a class called FooJob() which runs on a WCF windows service. This class has only 2 public methods, the constructor, and a Run() method.

当客户调用我的服务时,将 Job 类的一个新实例变暗,将一些参数传递给 ctor,然后调用 Run()...

When clients call my service, a Dim a new instance of the Job class, pass in some parameters to the ctor, then call Run()...

Run() 将获取参数,执行一些逻辑,向外部数据供应商发送(实时)请求,获取响应,执行一些业务逻辑,然后将其放入数据库...

Run() will take the parameters, do some logic, send a (real time) request to an outside data vendor, take the response, do some business logic, then put it in the database...

然后(如果可能的话)在 Run() 函数上只编写一个单元测试是否明智?还是我会在这里自杀?在这种情况下,我应该深入研究私有函数并对 FooJob() 类的函数进行单元测试吗?但是,这不会打破"某些人在 TDD 中主张的唯一测试行为"/公共接口范式吗?

Is it wise to only write a single unit test then (if even possible) on the Run() function? Or will I wind up killing myself here? In this case then should I drill into the private functions and unit test those of the FooJob() class? But then won't this 'break' the 'only test behavior' / public interface paradigm that some argue for in TDD?

我意识到这可能是一个含糊不清的问题,但我们将不胜感激任何建议/指导或正确方向的观点.

I realize this might be a vague question, but any advice / guidance or points in the right direction would be much appreciated.

画画

推荐答案

做一些逻辑,向外部数据供应商发送(实时)请求,获取响应,做一些业务逻辑,然后将其放入数据库

do some logic, send a (real time) request to an outside data vendor, take the response, do some business logic, then put it in the database

这里的问题是你已经将你的类列为具有多个职责......要真正进行单元测试,你需要遵循单一职责原则.您需要将这些职责提取到单独的接口中.然后,您可以单独(作为单元)测试这些接口的实现.如果您发现无法轻松测试您的班级正在做的事情,那么其他班级可能应该这样做.

The problem here is that you've listed your class as having multiple responsibilities... to be truly unit testable you need to follow the single responsibility principle. You need to pull those responsibilities out into separate interfaces. Then, you can test your implementations of these interfaces individually (as units). If you find that you can't easily test something your class is doing, another class should probably be doing that.

看来您至少需要以下内容:

It seems like you'd need at least the following:

  1. 业务逻辑接口.
  2. 定义对外部供应商的请求的接口.
  3. 数据存储库的界面.

然后,您可以分别测试该业务逻辑、与外部供应商通信的过程以及保存到数据库的过程.然后,您可以模拟这些接口来测试您的 Run() 方法,只需确保按预期调用这些方法即可.

Then you can test that business logic, the process of communicating with the outside vendor, and the process of saving to your database separately. You can then mock out those interfaces for testing your Run() method, simply ensuring that the methods are called as you expect.

要正确执行此操作,理想情况下应将类的依赖项(上面定义的接口)传递给其构造函数(即依赖项注入),但那是另一回事了.

To do this properly, the class's dependencies (the interfaces defined above) should ideally be passed in to its constructor (i.e. dependency injection), but that's another story.

这篇关于只在中等规模的班级上测试公共方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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