How to Unit Test void method with task inside

I have a graphical CancelChanges () method used and called by ViewModel. I want to test this method, but we have a task inside. We use the task not to freeze the user interface. My test method must wait for the result of this task to verify the result.

The code:

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;
                                }
                            });
                });
}

And my testing method:

/// <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);
}

How can we make my test expect the result of a task?

Thank.

+4
source share
2 answers

Make a CancelChangesreturn a method Task, and then wait for it, or configure the continuation in the test method. Some like it

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 await , .

, .

+6

, , Task.Run. , , , , :

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;
     }
   });
 });
}

:

[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);
}
+1

Source: https://habr.com/ru/post/1541759/


All Articles