我有一个名为EventManager的具体类和一个名为ScheduledEventManager的子类。我希望ScheduledEventManager必须传递与EventManager相同的测试以及一些额外的测试。这可能与xUnit.net有关吗?
编辑:我刚刚意识到我的情况比这复杂一点。我正在使用嵌套类来保持我的测试更有条理。例如:public class EventManagerTests
{
public class WhenAnEventIsFired
{
[Fact]
void ItNotifiesSubscribers()
{
// Perform the test
}
}
}
public class ScheduledEventManagerTests
{
// How to I inherit the above tests since they are in nested classes?
}
在我看来,这是不可能的,但也许你们中的一个天才知道我不知道的事情。
答案 0 :(得分:4)
是的,您可以:
public abstract class EventManagerTests
{
protected IEventManager _ev;
protected EventManagerTests(IEventManager ev)
{
_ev = ev;
}
[Fact]
public void SharedTest()
{
// Perform _ev test
}
}
public class ScheduledEventManagerTests : EventManagerTests
{
public ScheduledEventManagerTests():base(new ScheduledEventManager())
{
}
// It will inherit tests from the base abstract class
}
public class UnScheduledEventManagerTests : EventManagerTests
{
public UnScheduledEventManagerTests():base(new UnScheduledEventManager())
{
}
// It will inherit tests from the base abstract class
}
答案 1 :(得分:-2)
创建一个参数化测试,将基类的实例作为SUT,并使用子类的实例调用测试。这是一个使用NUnit的(人为的)示例,它导致一次通过和一次失败测试:
public class Foo
{
public virtual int DoSomething()
{
return 10;
}
}
public class Bar : Foo
{
public override int DoSomething()
{
return 9;
}
}
[TestFixture]
public class Tests
{
private Foo[] _foos = { new Foo(), new Bar() };
[Test]
[TestCaseSource("_foos")]
public void When_DoSomething_Is_Invoked_Then_A_Power_Of_Ten_Is_Returned(Foo sut)
{
Assert.That(sut.DoSomething() % 10, Is.EqualTo(0));
}
}