I am trying to unit test my viewmodel and I have many commands that simply create a new task and call a service method and using a continuation gets the results and binds them to properties on the viewmodel. I'm fairly new to unit testing and not sure how I would test this scenario. Since its running in a task the assert in my test happens before the service call finishes and before I can set any properties on my viewmodel. Is this how i should unit test my viewmodel?
public ICommand GetItems
{
if(this.Category != null)
{
Task<List<Item>> t = new Task<List<Item>>((o) =>
{
return _service.GetItems(this.Category);
}
t.ContinueWith((task) =>
{
this.Items = task.Result;
}
t.Start();
}
}
[TestMethod]
public void TestGetItems()
{
var selectedCategory = Category.NewItems;
var expected = new List<Item>(){ new Item(){ Value = "ExpectedValue" } };
var service = new Mock<IService>();
service.Setup(i => i.GetItems(selectedCategory)).Returns(expected);
var sut = new MainViewModel(_service.Object);
sut.Category = selectedCategory;
sut.GetItems.Execute(null);
Assert.AreEqual(expected, sut.Items);
}