Unit Testing in Silverlight

Unit Testing in Silverlight

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


Peabody posted on Monday, May 21, 2012

I am trying to test a viewmodel in my silverlight project.  The problem I am running into is that I cannot make any calls to the business layer until I have authenticated the user identity.  My plan was to authenticate in the test class initialization using AutoResetEvent to wait until authentication had happened before continuing with the unit tests like the following:

 

        [ClassInitialize]

        public void InitializeClass()

        {

            AutoResetEvent finishedEvent = new AutoResetEvent(false);

            Security.SLPrincipal.Login("testuser", "testpassword", (o, e) =>

            {

               finishedEvent.Set();

            });

            finishedEvent.WaitOne();

        }

 

In this code the loging method calls the async dataportal BeginFetch method.  The problem is that as soon as the code hits finishedEvent.WaitOne() the application freezes and never unfreezes.  I also set breakpoints in the server side code and confirmed that they are never being hit.  It is as if the client side is never getting off the call to the server before the WaitOne method is called.   I must have some fundamental misunderstanding of either the AutoResetEvent object or how exactly the async BeginFetch method works.   Can anybody clarify what I am doing wrong in this scenario?

 

RockfordLhotka replied on Monday, May 21, 2012

You really can't use that type of synchronization primitive in Silverlight, because a lot of things marshal back to the UI thread and you end up blocking yourself.

This is why we wrote and use UnitDriven (www.codeplex.com/unitdriven) for the CSLA unit tests - it provides a way to manage async test code that is the same on .NET and Silverlight.

Copyright (c) Marimer LLC