简体   繁体   English

测试单个接口的不同具体实现?

[英]Testing different concrete implementations of single interface?

I have the following code: 我有以下代码:

public interface IFoo
{
    IResult ResolveTheProblem(IBar inputData);
}

public class FastFoo : IFoo
{
    public IResult ResolveTheProblem(IBar inputData)
    {
        // Algorithm A - resolves the problem really fast
    }
}

public class SlowFoo : IFoo
{
    public IResult ResolveTheProblem(IBar inputData)
    {
        // Algorithm B - different algoritm, resolves the problem slow
    }
}

The most important thing to test is implementation of each algorithm. 要测试的最重要的事情是每种算法的实现。 For testing I'm using NUnit and NSubstitute. 为了进行测试,我使用了NUnit和NSubstitute。 Right now I have test like this: 现在我有这样的测试:

    [Test]
    public void FooTest()
    {
        IFoo foo = Substitute.For<IFoo>();
        IBar bar = Substitute.For<IBar>();

        IResult result = foo.ResolveTheProblem(bar);

        Assert.IsNotNull(result);
    }

My two questions: 我的两个问题:

  • Is that test even necessary? 那个测试甚至必要吗? I'm not sure about that 对此我不确定
  • How can I test implementation of each IFoo (FastFoo and SlowFoo)? 如何测试每个IFoo(FastFoo和SlowFoo)的实现?

EDIT: FastFoo and SlowFoo are two completely different implementations. 编辑:FastFoo和SlowFoo是两个完全不同的实现。 The result of both is a random number from 1 to 10. 两者的结果都是1到10之间的随机数。

No, it's not necessary. 不,这是没有必要的。 Why would you want to test a substitute implementation? 您为什么要测试替代实现? You substitute your dependencies, like IBar. 您可以替换依赖项,例如IBar。 You test your concrete implementations: 您测试您的具体实现:

[Test]    
public void SlowFooTest()
{    
    IBar bar = Substitute.For<IBar>();
    // Setup bar expectations / canned responses as required
    var foo = new SlowFoo(bar);
    IResult result = foo.ResolveTheProblem(bar);
    // Validate result from concrete class:
    Assert.IsNotNull(result);
}

[Test]    
public void FastFooTest()
{    
    IBar bar = Substitute.For<IBar>();
    var foo = new FastFoo(bar);
    IResult result = foo.ResolveTheProblem(bar);
    Assert.IsNotNull(result);
}

Is that test even necessary? 那个测试甚至必要吗? I'm not sure about that 对此我不确定

That test does not appear to do anything. 该测试似乎没有任何作用。 You appear to be creating a test double for the service under test. 您似乎正在为被测试的服务创建测试双。

How can I test implementation of each IFoo (FastFoo and SlowFoo)? 如何测试每个IFoo(FastFoo和SlowFoo)的实现?

Is the answer always going to be the same for FastFoo and SlowFoo, or does FastFoo trade accuracy for speed? FastFoo和SlowFoo的答案是否总是相同,还是FastFoo追求速度的准确性?

If they are always the same then inheritance. 如果它们始终相同,则继承。 Create a base FooTest with an abstract CreateFoo . 用抽象的CreateFoo创建一个基本的FooTest Then two concrete implementations. 然后是两个具体的实现。

If they are not always the same, then again inheritance but with a fuzzy element. 如果它们不总是相同,则再次继承,但带有模糊元素。

abstract class AbstractFooTester {
    [Test]
    public void WhenBarIsSomethingThenResultIsSomethingElse() {
         var mockRandomNumberGenerator = createRandomNumberMock(5);
         var mockBar = Substitute.For<IBar>();
         // set up Bar
         ...
        var subject = createFoo(mockRandomNumberGenerator);
        IResult result = subject.ResolveTheProblem(bar);

        AssertResult(result, ...);
    }
    abstract Foo createFoo(RandomNumberGenertor g);
    RandomNumberGenertor createRandomNumberMock(Int i) { ... }
}

class TestFastFoo extends AbstractFooTester {
      Foo createFoo(RandomNumberGenertor g) { return new FastFoo(g); }
}

class TestSlowFoo extends AbstractFooTester {
      Foo createFoo(RandomNumberGenertor g) { return new SlowFoo(g); }
}

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

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