C# 如何使用内部任务对void方法进行单元测试
我有一个图形方法CancelChanges(),由ViewModel使用和调用。 我想测试这个方法,但是我们有一个任务在里面。 我们使用任务来不冻结UI。 我的测试方法需要等待此任务的结果来检查结果 代码是:C# 如何使用内部任务对void方法进行单元测试,c#,unit-testing,task-parallel-library,C#,Unit Testing,Task Parallel Library,我有一个图形方法CancelChanges(),由ViewModel使用和调用。 我想测试这个方法,但是我们有一个任务在里面。 我们使用任务来不冻结UI。 我的测试方法需要等待此任务的结果来检查结果 代码是: public override void CancelChanges() { Task.Run( async () => { this.SelectedWorkflow =
public override void CancelChanges()
{
Task.Run(
async () =>
{
this.SelectedWorkflow = null;
AsyncResult<IncidentTypeModel> asyncResult = await this.Dataprovider.GetIncidentTypeByIdAsync(this.Incident.Id);
Utils.GetDispatcher().Invoke(
() =>
{
if (asyncResult.IsError)
{
WorkflowMessageBox.ShowException(
MessageHelper.ManageException(asyncResult.Exception));
}
else
{
this.Incident = asyncResult.Result;
this.Refreshdesigner();
this.HaveChanges = false;
}
});
});
}
public override void CancelChanges()
{
任务。运行(
异步()=>
{
this.SelectedWorkflow=null;
AsyncResult AsyncResult=等待this.Dataprovider.GetIncidentTypeByIdAsync(this.Incident.Id);
Utils.GetDispatcher().Invoke(
() =>
{
if(asyncResult.IsError)
{
WorkflowMessageBox.ShowException(
ManageException(asyncResult.Exception));
}
其他的
{
this.Incident=asyncResult.Result;
这个.Refreshdesigner();
this.HaveChanges=false;
}
});
});
}
我的测试方法是:
/// <summary>
/// A test for CancelChanges
/// </summary>
[TestMethod]
[TestCategory("ConfigTool")]
public void CancelChangesTest()
{
string storexaml = this._target.Incident.WorkflowXamlString;
this._target.Incident.WorkflowXamlString = "dsdfsdgfdsgdfgfd";
this._target.CancelChanges();
Assert.IsTrue(storexaml == this._target.Incident.WorkflowXamlString);
Assert.IsFalse(this._target.HaveChanges);
}
[TestMethod]
[TestCategory("ConfigTool")]
public async Task CancelChangesTest()
{
string storexaml = this._target.Incident.WorkflowXamlString;
this._target.Incident.WorkflowXamlString = "dsdfsdgfdsgdfgfd";
var cancelChanges = await this._target.CancelChanges();
Assert.IsTrue(storexaml == this._target.Incident.WorkflowXamlString);
Assert.IsFalse(this._target.HaveChanges);
}
//
///取消更改的测试
///
[测试方法]
[TestCategory(“ConfigTool”)]
public void CancelChangesTest()
{
string storexaml=this.\u target.Incident.WorkflowXamlString;
这是._target.Incident.WorkflowXamlString=“dsdfsdgffdsgdfgfd”;
此._target.CancelChanges();
Assert.IsTrue(storexaml==this.\u target.Incident.WorkflowXamlString);
Assert.IsFalse(this.\u target.HaveChanges);
}
我们怎样才能让我的测试等待任务的结果呢
谢谢。使
取消更改
方法返回一个任务
,然后等待此操作或在测试方法中设置一个延续。有些像这样
public override Task CancelChanges()
{
return Task.Factory.StartNew(() =>
{
// Do stuff...
});
}
注意从Task.Run
到Task.Factory.StartNew
的更改。在这种情况下,这是启动任务的更好方法。然后在测试方法上
[TestMethod]
[TestCategory("ConfigTool")]
public void CancelChangesTest()
{
string storexaml = this._target.Incident.WorkflowXamlString;
this._target.Incident.WorkflowXamlString = "dsdfsdgfdsgdfgfd";
this._target.CancelChanges().ContinueWith(ant =>
{
Assert.IsTrue(storexaml == this._target.Incident.WorkflowXamlString);
Assert.IsFalse(this._target.HaveChanges);
});
}
您还可以将测试方法标记为async
,并在测试方法中使用wait
,以执行相同的操作
我希望这能有所帮助。我会进一步进行重构,如果可能的话,避免使用
任务。运行。由于您所做的只是等待,然后调用UI线程上的工作,因此我将执行以下操作:
public override Task CancelChanges()
{
this.SelectedWorkflow = null;
AsyncResult<IncidentTypeModel> asyncResult = await this.Dataprovider.GetIncidentTypeByIdAsync(this.Incident.Id);
if (asyncResult.IsError)
{ WorkflowMessageBox.ShowException(MessageHelper.ManageException(asyncResult.Exception));
}
else
{
this.Incident = asyncResult.Result;
this.Refreshdesigner();
this.HaveChanges = false;
}
});
});
}
您能将返回类型设置为任务吗?另外,看起来您不需要任务。运行,因为GetIncidentTypeByIdAsync
是异步的(希望是非阻塞的)。如果您想同步执行,您也可以使用这个。\u target.CancelChanges().Wait()
。谢谢您的帮助。我们现在使用这个版本。它正在工作,用户界面也没有冻结。我建议任何有此问题的人花5分钟阅读此博客