我试图验证特定的 CancellationTokenSource 是否用作方法调用中的实际参数。
public void DataVerification(Object sender, EventArgs e)
{
_entity.PopulateEntityDataVerificationStage(_view.DataTypeInputs, _view.ColumnNameInputs, _view.InitialRow, _view.FinalRow, _view.CurrencyPair, _view.CsvFilePath, _view.ErrorLogFilePath);
//...
CancellationTokenSource tempCsvFileVerificationCancellation = new CancellationTokenSource();
_source.Source = tempCsvFileVerificationCancellation;
//Want to verify that TempCsvFileVerificationCancellation.Token is passed into the following method.
_verify.SetupCsvFileVerification(_entity, tempCsvFileVerificationCancellation.Token);
//...
}
以下是我的测试:
[Test]
public void DataVerification_SetupCsvFileVerification_CorrectInputs()
{
Mock<IMainForm> view = new Mock<IMainForm>();
Mock<IUserInputEntity> entity = new Mock<IUserInputEntity>();
Mock<ICsvFileVerification> verify = new Mock<ICsvFileVerification>();
verify.Setup(x => x.SetupCsvFileVerification(It.IsAny<UserInputEntity>(), It.IsAny<CancellationToken>()));
CancellationTokenSource cts = new CancellationTokenSource();
Mock<ICancellationTokenSource> source = new Mock<ICancellationTokenSource>();
source.SetupSet(x => x.Source = It.IsAny<CancellationTokenSource>()).Callback<CancellationTokenSource>(value => cts = value);
source.SetupGet(x => x.Source).Returns(cts);
source.SetupGet(x => x.Token).Returns(cts.Token);
MainPresenter presenter = new MainPresenter(view.Object, entity.Object, verify.Object, source.Object);
presenter.DataVerification(new object(), new EventArgs());
verify.Verify(x => x.SetupCsvFileVerification(entity.Object, source.Object.Token));
}
错误信息如下:
对模拟的预期调用至少一次,但从未执行:x => x.SetupCsvFileVerification(.entity.Object, (Object).source.Object.Token) 未配置任何设置。
source代表的类如下:
public interface ICancellationTokenSource
{
void Cancel();
CancellationTokenSource Source { get; set; }
CancellationToken Token { get; }
}
public class CancellationTokenSourceWrapper : ICancellationTokenSource
{
private CancellationTokenSource _source;
public CancellationTokenSourceWrapper(CancellationTokenSource source)
{
_source = source;
}
public CancellationTokenSource Source
{
get
{
return _source;
}
set
{
_source = value;
}
}
public CancellationToken Token
{
get
{
return Source.Token;
}
}
public void Cancel()
{
_source.Cancel();
}
}
当我逐步完成单元测试时,cts 确实被分配了 TempCsvFileVerificationCancellation 的值。source 中的 Token 属性,返回 Source.Token。我不知道我做错了什么。
任何指针/帮助将不胜感激。
谢谢
编辑