如何分配模拟的抽象类

时间:2017-08-16 10:54:44

标签: c# unit-testing abstract-class nsubstitute

我知道这个问题有点被问到,但我相信我对其他问题/答案有一个变体。

我有这个C#类:

public class  public CoreCycleTimer: MyAbstractClass
{
    public CoreCycleTimer()
    {
         if ( !Initialised ) 
        {
            Initialise( Path.Combine( settingsService.BaseDirectory, TimingDataFile ) );
        }
        WriteLine( "Initialised!" );
    }
}

我的抽象类:

public abstract class MyAbstractClass
{
    private FileStream fs;
    private StreamWriter stream;

    public bool Initialised { get; private set; }

    public void Initialise(
        string filePath )
    {
         fs = File.Open( filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite );
        stream = new StreamWriter( fs );
        Initialised = true;
    }

    public void WriteLine(
        string stringToWrite )
    {
        stream.WriteLine( stringToWrite );
        Flush();
    }
}

我试图通过以下方式测试:

protected MyAbstractClass MockStreamWriterBuilderService;
protected CoreCycleTimer SUT;

protected override void Because()
{
    MockStreamWriterBuilderService = Substitute.For<MyAbstractClass>();
    SUT = new CoreCycleTimer();
}

[Test]
public void Then_An_IStreamWriterService_Is_Initialised()
{
    MockStreamWriterBuilderService.Received( 1 ).Initialise( Arg.Any<string>() );
}

但当然MockStreamWriterBuilderService是空的&#39; (请参阅截图)。我该怎么办呢?

NB: 我已经缩写了一些处理dispose,错误处理等的代码 enter image description here

0 个答案:

没有答案