Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Unit Test a ViewModel with async method.
    primarykey
    data
    text
    <p>I am not sure where to start but let me give you a brief idea on where I am and what I want to achieve. I am quite new to Unit Testing on MVVM and having difficulty on testing the commands that I exposed using PRISM delegate command properties. My delegate commands calls async method that has to be waited so I can get the actual result. Below is an asyc method that is called by method that I wanted to test.</p> <pre><code> async void GetTasksAsync() { this.SimpleTasks.Clear(); Func&lt;IList&lt;ISimpleTask&gt;&gt; taskAction = () =&gt; { var result = this.dataService.GetTasks(); if (token.IsCancellationRequested) return null; return result; }; IsBusyTreeView = true; Task&lt;IList&lt;ISimpleTask&gt;&gt; getTasksTask = Task&lt;IList&lt;ISimpleTask&gt;&gt;.Factory.StartNew(taskAction, token); var l = await getTasksTask; // waits for getTasksTask if (l != null) { foreach (ISimpleTask t in l) { this.SimpleTasks.Add(t); // adds to ViewModel.SimpleTask } } } </code></pre> <p>also here is the command in my VM that calls the async method above</p> <pre><code> this.GetTasksCommand = new DelegateCommand(this.GetTasks); void GetTasks() { GetTasksAsync(); } </code></pre> <p>and now my Test Method goes like </p> <pre><code> [TestMethod] public void Command_Test_GetTasksCommand() { MyViewModel.GetTaskCommand.Execute(); // this should populate ViewModel.SimpleTask Assert.IsTrue(MyBiewModel.SimpleTask != null) } </code></pre> <p>Currently what I am getting is that my ViewModel.SimpleTask = null this is because it does not wait for the async method to finish. I understand there are some related topics to this already in stack overflow but I could not find something related to my DelegateCommands. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload