对 CSLA 异步验证规则进行单元测试

发布于 2024-10-15 21:25:14 字数 2423 浏览 2 评论 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 可以为此提供帮助吗?我已经看过它,但不知道如何在这种情况下使用它。

谢谢, 汤姆

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千寻… 2024-10-22 21:25:14

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 内部。

谢谢,
汤姆

Answered by JonnyBee on 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();
        }
    }
}

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

Thanks,
Tom

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文