This project is read-only.

Unit Testing Samples

Jan 13, 2012 at 9:10 PM

Are there any unit test samples for this out there?

Jan 19, 2012 at 3:37 AM

There's a unit test project for WPF as part of the ProjectTracker sample app for CSLA .NET version 4.1 and higher. That's probably the best source for ideas.

Jan 19, 2012 at 7:58 PM


Thanks for the response.  I've found tests in the latest 4.2.2 samples.  I really like the ProjectTracker.Test.Library unit tests for the actual CSLA classes.  I'm a little stumped at the WpfUITest project.  It looks like it is a shell for doing the unit tests.  Are there any examples where you are testing any of the actual ViewModels?  I guess I was expecting to open ViewModel and call the public commands and verifiy they work..?  Does this makes sense?

Thanks again for taking the time to respond, I know you are busy.


Jan 19, 2012 at 8:20 PM

The MainPresenterTests class contains the ShowMenu test method to demonstrate how to test a viewmodel.

The hard part is setting up the shell, and that's the primary focus of the project. You need to set up the shell as described in the Using CSLA 4: WPF and Silverlight book, and shown in the WpfUI.Test project - then you can build tests.

It is true that I don't have a comprehensive set of tests in WpfUI.Test. My purpose isn't to test the sample app though, it is to demonstrate what's necessary to get to the point you can write tests.

Jan 25, 2012 at 7:36 PM


Thanks again.  It took me a little bit to figure this one out.  I've recently been writing silverlight unit tests for my business objects (arg, async!).  It just took me some time to get what you were doing in the test project.  I just got my first couple of mvvm tests working!  Thanks!


Jan 25, 2012 at 9:18 PM

Just hit a snag.  I call a method on viewmodel that loads a new view/viewmodel and that works great.  However, now I want to manipulate this new viewmodel but the viewmodel's .model property is null because it loads asynch.  How the heck do I know when it loads?  I've tried dropping a ManualResetEvent based on the status events but it looks like I get deadlocked.  Any ideas?  I don't see any events outside the viewmodel to know when it is loaded either.  Asynch is great for users but is a hassle when you want to run tests serially.  Any ideas would be appreciated.



Jan 25, 2012 at 9:24 PM

That depends on how you build your viewmodel and how your testing framework handles async behaviors - so I can't give you a specific answer.

But for an example, the CSLA .NET ViewModelBase class raises a PropertyChanged event when the Model property changes, and you can use that to know when the async load is complete. The UnitDriven test framework we use for the CSLA unit tests has a technique designed to allow you to wait (with a lambda callback) for the async operation to complete, process the various Assert calls, and then tell UnitDriven that the test is actually finished.

Other testing frameworks probably handle that scenario differently, but they all have some technique for dealing with async operations. Similarly, your viewmodel types may be constructed differently - the key is to have some event or other callback scheme that you can use to be notified when the async operation is complete.

Jan 26, 2012 at 6:53 PM

That makes sense.  I'm using the Silverlight Unit Testing stuff built-in to the official SL4/SL4 Toolkit.  There are methods to wait for async operations.  Looking for the Model PropertyChanged is going to ge the way to go I think.  Thanks again for all of your help!