Unit-tests on ViewModel with Asynchronous Method unable to correctly set Model-property (CSLA 4.3.12.0)

Unit-tests on ViewModel with Asynchronous Method unable to correctly set Model-property (CSLA 4.3.12.0)

Old forum URL: forums.lhotka.net/forums/t/11573.aspx


kgulden posted on Tuesday, September 11, 2012

I want to test methods on the viewmodel but the .Model property always raises the System.InvalidOperationException: "The calling thread cannot access this object because a different thread owns it." in the asynchronously-QueryCompleted method of ViewModelBase<T>. 

From ProjectTacker
-----------------

#1  public class ProjectGetter : ViewModel<ProjectTracker.Library.ProjectGetter>
#2  {
#3    public ProjectGetter()
#4    {
#5      BeginRefresh(callback => ProjectTracker.Library.ProjectGetter.CreateNewProject(callback));
#6    }
#7    ...
#8    ...
#9  }

From my UnitTest
-----------------

#1  ProjectGetter vm = new ProjectGetter();
#2  Assert.IsNotNull(vm);
#3  Assert.IsNotNull(vm.Child[0]);
#4  Assert.AreEqual(vm.Child[0].GetType(), typeof(ProjectGetter.ProjectEdit));


When I rewrite the ProjectTracker Line#5 and replace BeginRefresh with DoRefresh which is synchronous, the .Model property is properly populated.

I tried waiting on the PropertyChanged event of the ViewModel, but, alas, it is never raised.  There does not seem to be a way to properly wait on some event/callback method when the asynchronous method completes. I am at a loss and any help anyone can provide would be much appreciated.

(I have properly used the UnitDriven test framework to test synch/asynch BO's with much success, however, these asynch-viewmodels have me stuck.)

Copyright (c) Marimer LLC