简体   繁体   English

"如何对适配器类进行单元测试?"

[英]How do I unit test an adapter class?

Suppose I have the following classes from a third-party library:假设我有来自第三方库的以下类:

public class ThirdPartyType { ... }

public class ThirdPartyFunction
{
    public ThirdPartyType DoSomething() { ... }
}

The implementation details are not important, and they are actually outside of my control for this third-party library.实现细节并不重要,它们实际上不在我对这个第三方库的控制范围内。

Suppose I write an adapter class for ThirdPartyFunction :假设我为ThirdPartyFunction编写了一个适配器类:

public class Adapter
{
    private readonly ThirdPartyFunction f;

    public Adapter()
    {
        f = new ThirdPartyFunction();
    }

    public string DoSomething()
    {
        var result = f.DoSomething();

        // Convert to a type that my clients can understand
        return Convert(result);
    }

    private string Convert(ThirdPartyType value)
    {
        // Complex conversion from ThirdPartyType to string
        // (how do I test this private method?)
        ...
    }
}

How can I test that my implementation of Convert(ThirdPartyType) is correct?如何测试我的Convert(ThirdPartyType)实现是否正确? It's only needed by the Adapter class, which is why it's a private method.只有Adapter类才需要它,这就是它是私有方法的原因。

I would recommend extracting the code to a seprate class and then testing that class.我建议将代码提取到一个单独的类,然后测试该类。 Although it is only used by this Adapter it shouldn't be the responsibility of the adapter to do the conversion as well (in keeping with the Single Responsibility Principle).尽管它仅由该Adapter使用,但它不应该由Adapter负责进行转换(与单一职责原则保持一致)。

By extracting it out it gives you the ability to test the converter in isolation from the third party code.通过提取它,您可以独立于第三方代码测试转换器。

If the converter does not require any state you could also make it a static class and then reference it directly in your adapter without the need for registering it with dependency injection.如果转换器不需要任何状态,您也可以将其设为静态类,然后直接在您的适配器中引用它,而无需使用依赖注入注册它。

If you think about it, the adapter doesn't need testing (as it is just a wrapper) but the converter does - so extracting it to another class makes sense to allow it to be tested, even if it does mean another class in the code.如果您考虑一下,适配器不需要测试(因为它只是一个包装器)但转换器需要 - 因此将其提取到另一个类以允许对其进行测试是有意义的,即使它确实意味着另一个类代码。

In addition, extracting the converter to a separate class means that if the ThirdPartyType or the format of your string changes then you can make the changes without affecting the Adapter implementation.此外,将转换器提取到单独的类意味着如果ThirdPartyTypestring的格式发生更改,那么您可以在不影响Adapter实现的情况下进行更改。

If you change your Adapter class to allow the ThirdPartyFunction f to be passed to it, then you can use a mock version of it in your test class.如果您更改 Adapter 类以允许将 ThirdPartyFunction f 传递给它,那么您可以在测试类中使用它的模拟版本。 this will allow you to test the Convert function.这将允许您测试转换功能。

I'm not sure of the exact syntax as I am not familiar with the language, but I will give it a try:由于我不熟悉该语言,因此我不确定确切的语法,但我会尝试一下:

public class Adapter
{
  private readonly ThirdPartyFunction f;

  // pass the function to the constructor when creating the adapter
  public Adapter(ThirdPartyFunction inputF)
    {
      f = inputF;
    }

  public string DoSomething()
  {
    var result = f.DoSomething();

    // Convert to a type that my clients can understand
    return Convert(result);
  }

  private string Convert(ThirdPartyType value)
  {
    // Complex conversion from ThirdPartyType to string
    // (how do I test this private method?)
    ...
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM