Unit Testing CSLA-based Silverlight Business Objects

Unit Testing CSLA-based Silverlight Business Objects

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


dpk posted on Friday, March 05, 2010

One thing I'm still hung up on is unit testing a CSLA-based business object for Silverlight. Can you point me to some example code in the downloads? Specifically I'm trying to understand how to deal with creating a new BO using the async factory methods. Perhaps this is not strictly a CSLA question. I'm playing around with ManualResetEvent but I think I'm missing something.

If I wanted to test inserting an object, I have to create one first. Here's my test code:

        private static ManualResetEvent waitHandle;

 

        public static Role CreateObject()

        {

            waitHandle = new ManualResetEvent(false);

            waitHandle.Reset();

 

            Role obj = null;

            Role.NewRole((o, e) =>

            {

                obj = e.Object;

                obj.Name = "Name";

                obj.Description = "Description";

                waitHandle.Set();

            });

            bool result = waitHandle.WaitOne(5000);

            if (result)

                return obj;

            else

                throw new Exception("CreateObject timed out.");

        }

 

        [TestMethod]

        public void Insert()

        {

            UnitTestContext context = GetContext();

            var obj = CreateObject();

            waitHandle = new ManualResetEvent(false);

            obj.BeginSave((o, e) =>

            {

                var savedObject = (Role)e.NewObject;

                context.Assert.IsNull(e.Error);

                context.Assert.IsNotNull(savedObject);

                context.Assert.IsFalse(savedObject.IsNew);

                context.Assert.IsFalse(savedObject.IsDirty);

                context.Assert.IsFalse(savedObject.IsDeleted);

                context.Assert.Success();

            });

        }

The problem is that the waitHandle times out.


Now, my test code may not be correct yet and I haven't introduced any mocking and such yet. I'm just trying a proof-of-concept of sorts to see if I can get a single object to work from UI to DB and back and all of the layers in between.

Thanks,
Dave

RockfordLhotka replied on Friday, March 05, 2010

There are several SL unit test frameworks out there now. I haven't looked recently to see how they all work. When we created CSLA .NET for Silverlight originally, there were no real unit test frameworks - just a prototype from Microsoft. And it didn't meet our core needs:

  1. Support testing async operations
  2. Support the exact same test code on SL and .NET

So we created our own: UnitDriven.

I suggest you look at the unit tests for CSLA .NET for Silverlight - those tests span a wide range of areas obviously, but there are stereotype tests, which are testing the various business object stereotypes, and thus are somewhat similar to the kind of tests you are probably hoping to write.

If you don't use UnitDriven you'll obviously need to adapt the async testing concepts into the async testing support provided by your unit test tool. If your unit test tool doesn't support testing async operations then I strongly suggest you find a better test tool.

Copyright (c) Marimer LLC