静态方法:在不用时 [英] Static Methods : When and when not

查看:120
本文介绍了静态方法:在不用时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的TDD和DDD和我有一个关于静态方法,一般一个简单的问题。大多数TDD的大师们说,在一个字静态方法是不好的(以及我们应该忘记创造万吨静态实用工具,我们(UM或I)中使用,因为它们是不可测试进行之前,我能明白为什么它们是不可测试(一个伟大的澄清文章,可以发现这里了解那些有兴趣谁,但我想我是唯一的小白在这里:(),但我不知道是否有一个非常干净的使用指南从静态的角度TDD点?

I am new to TDD and DDD and I have one simple question regarding static methods in general. Most of the gurus of TDD says in one word that static methods are bad (and that we should forget about creating tons of static utilities that we (um or I) used to make before as they are not testable. I can see why they are not testable ( a great clarification article can be found here for those who are interested but I guess I am the only noob here :( ) but I was wondering is there a nice and clean guideline for using statics from TDD point of view?

这可能是你最真的愚蠢的问题,但有些技巧将是巨大的,我只是想知道专家是怎么想到这里的静态的东西。提前致谢。

This may be really silly question for most of you but some tips would be great and I just want to know how experts here think of static stuff. Thanks in advance.

编辑:在寻找答案,我发现2关于静态(不TDD的关注虽然)使用其他不错的线程我的猜测是好读为那些有兴趣谁(包括我自己)

While looking for answer I found 2 other nice threads regarding usage of static ( not TDD concern though) which I guess are good reads for those who are interested(myself inclusive).

  • Topic 1
  • Topic 2

推荐答案

我想你可能已经稍有误解。

I think you may have slightly misunderstood.

静态方法是测试。以这种方法为例:

Static methods are testable. Take this method as an example:

public static int Add(int x, int y)
{
    return x + y;
}

您可以通过测试返回值是根据传入的参数是什么,你希望对此进行测试。

You can test this by testing that the return value is what you expect based on the arguments passed in.

如果静态方法成为麻烦的时候测试是当你需要引入一个模拟。

Where static methods become troublesome when testing is when you need to introduce a mock.

让我们说我有一些code调用 File.Delete()静态方法。为了不依赖于文件系统上测试我的code,我将要替换/嘲笑这个调用与只是验证它被称为从code正在测试一个测试版。这是很容易做到,如果我对其中的对象中删除()正在所谓的一个实例。大多数(全部?)嘲讽框架不能模拟静态方法,所以采用静态方法在我的code迫使我不同的测试(通常通过调用真正的静态方法)。

Let's say I have some code that calls the File.Delete() static method. To test my code without relying on the file system, I would want to replace/mock this call with a test version that just verifies that it has been called from the code being tested. This is easy to do if I had an instance of an object on which Delete() was being called. Most (all?) mocking frameworks can not mock static methods, so using a static method in my code forces me to test it differently (usually by calling the real static method).

要测试这样的事情,我想介绍一个接口:

To test something like this, I would introduce an interface:

interface IFileDeleter
{
    void Delete(string file);
}

我的code会再取一个对象来实现这个接口(无论是在方法调用或作为构造函数参数)的实例,然后调用它的删除()办法做删除:

void MyMethod(string file)
{
    // do whatever...
    deleter.Delete(file);
}

要测试这一点,我可以使 IFileDeleter 接口的模拟,并简单地验证其删除()方法已经被调用。这消除了需要有一个真正的文件系统作为测试的一部分。

To test this, I can make a mock of the IFileDeleter interface and simply verify that its Delete() method had been called. This removes the need to have a real file system as part of the test.

这可能看起来像code更复杂(这是),但它使得它更容易测试为自己支付。

This may look like the code is more complex (which it is), but it pays for itself in making it far easier to test.

这篇关于静态方法:在不用时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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