测试单个接口的不同具体实现?
Testing different concrete implementations of single interface?
我有以下代码:
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
}
}
最重要的测试是每个算法的实现。
为了进行测试,我使用了 NUnit 和 NSubstitute。现在我有这样的测试:
[Test]
public void FooTest()
{
IFoo foo = Substitute.For<IFoo>();
IBar bar = Substitute.For<IBar>();
IResult result = foo.ResolveTheProblem(bar);
Assert.IsNotNull(result);
}
我的两个问题:
- 这个测试有必要吗?我不确定
- 如何测试每个 IFoo(FastFoo 和 SlowFoo)的实现?
编辑:FastFoo 和 SlowFoo 是两个完全不同的实现。两者的结果都是一个从1到10的随机数。
不,没有必要。为什么要测试替代实现?
您替换您的依赖项,例如 IBar。
你测试你的具体实现:
[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
该测试似乎没有做任何事情。您似乎正在为被测服务创建一个测试替身。
How can I test implementation of each IFoo (FastFoo and SlowFoo)?
FastFoo 和 SlowFoo 的答案总是相同,还是 FastFoo 会以准确性换取速度?
如果它们始终相同则继承。用抽象 CreateFoo
创建一个基础 FooTest
。然后是两个具体的实现。
如果它们不总是相同,则再次继承,但带有模糊元素。
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); }
}
我有以下代码:
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
}
}
最重要的测试是每个算法的实现。 为了进行测试,我使用了 NUnit 和 NSubstitute。现在我有这样的测试:
[Test]
public void FooTest()
{
IFoo foo = Substitute.For<IFoo>();
IBar bar = Substitute.For<IBar>();
IResult result = foo.ResolveTheProblem(bar);
Assert.IsNotNull(result);
}
我的两个问题:
- 这个测试有必要吗?我不确定
- 如何测试每个 IFoo(FastFoo 和 SlowFoo)的实现?
编辑:FastFoo 和 SlowFoo 是两个完全不同的实现。两者的结果都是一个从1到10的随机数。
不,没有必要。为什么要测试替代实现? 您替换您的依赖项,例如 IBar。 你测试你的具体实现:
[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
该测试似乎没有做任何事情。您似乎正在为被测服务创建一个测试替身。
How can I test implementation of each IFoo (FastFoo and SlowFoo)?
FastFoo 和 SlowFoo 的答案总是相同,还是 FastFoo 会以准确性换取速度?
如果它们始终相同则继承。用抽象 CreateFoo
创建一个基础 FooTest
。然后是两个具体的实现。
如果它们不总是相同,则再次继承,但带有模糊元素。
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); }
}