Unit Test a CSLA Asynchronous Validation Rule

Unit Test a CSLA Asynchronous Validation Rule

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


tomhunter posted on Thursday, February 03, 2011

I have a validation rule on a CSLA Business Base stereotyped class. I'm having trouble figuring out how to unit test the validation rule as it includes an asynchronous callback lambda expression. Here's some example code:

using System;
using System.Collections.Generic;
using Csla;
using Csla.Validation;

namespace UnitTestCSLAAsyncValidationRule
{
    public class BusinessObject : BusinessBase<BusinessObject>
    {
        protected static PropertyInfo<string> CodeProperty = RegisterProperty<string>(p => p.Code);
        public string Code
        {
            get { return GetProperty(CodeProperty); }
            set { SetProperty(CodeProperty, value); }
        }

        protected override void AddBusinessRules()
        {
            ValidationRules.AddRule(CodeValidator, new AsyncRuleArgs(CodeProperty));
        }

        public static void CodeValidator(AsyncValidationRuleContext context)
        {
            var code = (string) context.PropertyValues["Code"];

            CodeList codeList;
            CodeList.GetCodeList((o, l) =>
                {
                    codeList = l.Object;
                    if (codeList.Contains(code))
                    {
                        context.OutArgs.Result = false;
                        context.OutArgs.Description = "Code already in use.";
                    }
                    else
                    {
                        context.OutArgs.Result = true;
                    }
                });

            context.Complete();

        }
    }

    public class CodeList : List<string>
    {
        public static void GetCodeList(EventHandler<DataPortalResult<CodeList>> handler)
        {
            DataPortal<CodeList> dp = new DataPortal<CodeList>();
            dp.FetchCompleted += handler;
            dp.BeginFetch();
        }

        private void DataPortal_Fetch()
        {
            // some existing codes..
            Add("123");
            Add("456");
        }
    }
}

I would like to test this with a test similar to the following:

using NUnit.Framework;

namespace UnitTestCSLAAsyncValidationRule.Test
{
    [TestFixture]
    public class BusinessObjectTest
    {
        [Test]
        public void CodeValidationTest()
        {
            var bo = new BusinessObject();
            bo.Code = "123";

            Assert.IsNotEmpty(bo.BrokenRulesCollection);
        }
    }
}

However, the test Assert runs before the async callback. Is this something UnitDriven could help with? I've had a look at it but can't see how to use it in this scenario.

Thanks, Tom

JonnyBee replied on Thursday, February 03, 2011

Like this:

    [TestMethod]
    public void TestAsyncRulesAndSyncRulesValid()
    {
      UnitTestContext context = GetContext();

      var har = new AsyncRuleRoot();

      har.ValidationComplete += (o, e) =>
      {
        context.Assert.IsFalse(string.IsNullOrEmpty(har.CustomerNumber));
        context.Assert.IsFalse(string.IsNullOrEmpty(har.CustomerName));

        context.Assert.IsTrue(har.IsValid, "IsValid 2");
        context.Assert.Success();
      };
// this triggers the async rule
      har.CustomerNumber = "123456";

      context.Complete();
    }

 

tomhunter replied on Friday, February 04, 2011

Thanks JonnyBee!

Please not there was a small bug in my validation rule method - the call to AsyncValidationRuleContext.Complete() needs to be inside the lambda.

Copyright (c) Marimer LLC