Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用内部任务对void方法进行单元测试_C#_Unit Testing_Task Parallel Library - Fatal编程技术网

C# 如何使用内部任务对void方法进行单元测试

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 =

我有一个图形方法CancelChanges(),由ViewModel使用和调用。 我想测试这个方法,但是我们有一个任务在里面。 我们使用任务来不冻结UI。 我的测试方法需要等待此任务的结果来检查结果

代码是:

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分钟阅读此博客