0

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

但是,测试断言在异步回调之前运行。这是 UnitDriven 可以提供帮助的吗?我看过它,但看不到在这种情况下如何使用它。

谢谢,汤姆

4

1 回答 1

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 中。

谢谢,
汤姆

于 2011-02-04T10:35:39.100 回答