单元测试CSLA异步验证规则

时间:2011-02-03 16:09:55

标签: unit-testing asynchronous nunit csla unitdriven

我对CSLA Business Base构造型类有一个验证规则。我无法弄清楚如何对验证规则进行单元测试,因为它包含一个异步回调lambda表达式。这是一些示例代码:

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");
        }
    }
}

我想通过类似以下的测试对此进行测试:

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);
        }
    }
}

但是,测试Assert在异步回调之前运行。这是UnitDriven可以提供帮助吗?我已经看了一下,但在这种情况下无法看到如何使用它。

谢谢, 汤姆

1 个答案:

答案 0 :(得分:0)

JonnyBee在http://forums.lhotka.net/forums/p/10023/47030.aspx#47030上的回答:

using NUnit.Framework;
using UnitDriven;

namespace UnitTestCSLAAsyncValidationRule.Test
{
    [TestFixture]
    public class BusinessObjectTest : TestBase
    {
        [Test]
        public void CodeValidationTest()
        {
            UnitTestContext context = GetContext();

            var bo = new BusinessObject();
            bo.ValidationComplete += (o, e) =>
                {
                    context.Assert.IsFalse(bo.IsValid);
                    context.Assert.Success();
                    //Assert.IsNotEmpty(bo.BrokenRulesCollection);
                };

            bo.Code = "123";

            context.Complete();
        }
    }
}

请注意,我的验证规则方法中没有一个小错误 - 对AsyncValidationRuleContext.Complete()的调用需要在lambda中。

谢谢,
汤姆