Код: Выделить всё
public abstract class Base
{
public bool IsDone { get; set; } = false;
public abstract void Do();
}
public sealed class Derived : Base
{
public override void Do()
{
IsDone = true;
}
}
< pre class="lang-cs Prettyprint-override">
Код: Выделить всё
public abstract class BaseTest
{
protected abstract Base Sut { get; } // must return the same instance for every invocations, but how?
[Fact]
public void IsDone_Should_Be_True()
{
Sut.Do();
Assert.True(Sut.IsDone);
}
}
Код: Выделить всё
public class DerivedTest_CorrectlyImplemented_1 : BaseTest
{
private readonly Derived d = new Derived();
protected override Base Sut => d;
}
public class DerivedTest_CorrectlyImplemented_2 : BaseTest
{
protected override Base Sut { get; } = new Derived();
}
Код: Выделить всё
public class DerivedTest_WronglyImplemented : BaseTest
{
protected override Base Sut => new Derived();
}
Как мне спроектировать BaseTest так, чтобы DerivedTest не мог быть неправильно реализован (возвращая разные экземпляры для каждого получить вызов)?
Подробнее здесь: https://stackoverflow.com/questions/790 ... ing-the-sa