我知道这个问题有点被问到,但我相信我对其他问题/答案有一个变体。
我有这个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; (请参阅截图)。我该怎么办呢?