S#arp Arch 和 TDD

发布于 2024-10-11 16:33:40 字数 5920 浏览 2 评论 0原文

我正在尝试加快 TDD 的步伐,因此我创建了一个新的 S#arp Arch 应用程序 (v1.6),并一直在为我的课程创建测试。不幸的是,我已经遇到了一个尚未解决的问题。

我已经为“User”和“RecordRequest”类创建了比较测试。当使用 NUnit 执行测试时,“User”测试通过,而“RecordRequest”则未通过。我试图理解为什么一个测试通过而另一个测试没有通过。错误消息是:

在 e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Testing.NUnit\SyntaxHelpers.cs 中的 SharpArch.Testing.NUnit.SyntaxHelpers.ShouldEqual(实际对象,预期对象):第 25 行 在 D:\Web\Mvc\Projects\Campus Counsel\IRS\ISPS\tests\ISPS.Tests\ISPS.Core\Account\RecordRequestTests.cs 中的 Tests.ISPS.Core.Account.RecordRequestTests.CanCompareRecordRequests():第 23 行

'用户'类和测试:

using System;
using System.Security.Principal;
using NHibernate.Validator.Constraints;
using SharpArch.Core.DomainModel;

namespace ISPS.Core.Account
{
    public class User : Entity, IPrincipal
    {
        public static User DefaultUser()
        {
            return new User
            {
                Role = Role.Administrator,
                IsActive = true,
                CreatedOn = DateTime.Now
            };
        }

        [DomainSignature]
        [NotNullNotEmpty(Message = "Kerberos must be provided.")]
        public virtual string Kerberos { get; set; }

        [NotNullNotEmpty(Message = "First name must be provided.")]
        public virtual string FirstName { get; set; }

        [NotNullNotEmpty(Message = "Last name must be provided.")]
        public virtual string LastName { get; set; }

        [DomainSignature]
        [NotNullNotEmpty(Message = "Email must be provided.")]
        public virtual string Email { get; set; }

        [NotNull(Message = "Role must be provided.")]
        public virtual Role Role { get; set; }

        public virtual bool IsActive { get; set; }
        public virtual DateTime CreatedOn { get; set; }
        public virtual DateTime? ModifiedOn { get; set; }

        public virtual bool IsAdministrator { get { return Role.Id == Role.AdministratorId; } }
        public virtual bool IsEditor { get { return Role.Id == Role.EditorId; } }
        public virtual bool IsGuest { get { return Role.Id == Role.GuestId; } }

        public virtual IIdentity Identity
        {
            get
            {
                var isAuthenticated = (Role.Name == Role.Administrator.Name);
                return new Identity(isAuthenticated, Kerberos);
            }
        }

        public virtual bool IsInRole(string role)
        {
            return Role.Name == role;
        }
    }

    /// <summary>
    /// Simple implementation of IIdentity
    /// </summary>
    public class Identity : IIdentity
    {
        public Identity(bool isAuthenticated, string name)
        {
            IsAuthenticated = isAuthenticated;
            Name = name;
        }

        public string AuthenticationType
        {
            get { return "Forms"; }
        }

        public bool IsAuthenticated { get; private set; }

        public string Name { get; private set; }
    }
}

using System;
using NUnit.Framework;
using SharpArch.Testing.NUnit;
using ISPS.Core.Account;

namespace Tests.ISPS.Core.Account
{
    [TestFixture]
    public class UserTests
    {
        [Test]
        public void CanCompareUsers()
        {
            var instance = new User
                               {
                                   Kerberos = "nchomsky",
                                   FirstName = "Noam",
                                   LastName = "Chomsky",
                                   Email = "[email protected]",
                                   Role = Role.Administrator,
                                   IsActive = true,
                                   CreatedOn = DateTime.Parse("1/1/1975 10:00 AM"),
                                   ModifiedOn = null
                               };

            var instanceToCompareTo = new User
                                          {
                                              Kerberos = "nchomsky",
                                              FirstName = "Noam",
                                              LastName = "Chomsky",
                                              Email = "[email protected]",
                                              Role = Role.Administrator,
                                              IsActive = true,
                                              CreatedOn = DateTime.Parse("1/1/1975 10:00 AM"),
                                              ModifiedOn = null
                                          };

            instance.ShouldEqual(instanceToCompareTo);
        }
    }
}

'RecordRequest'类和测试:

using NHibernate.Validator.Constraints;
using SharpArch.Core.DomainModel;

namespace ISPS.Core.Requests
{
    public class RecordRequest : Entity
    {
        public static RecordRequest DefaultRecordRequest()
        {
            return new RecordRequest();
        }

        [NotNullNotEmpty(Message = "A description of the documents requested must be provided.")]
        public virtual string Description { get; set; }
    }
}

using ISPS.Core.Requests;
using NUnit.Framework;
using SharpArch.Testing.NUnit;

namespace Tests.ISPS.Core.Requests
{
    [TestFixture]
    public class RecordRequestTests
    {
        [Test]
        public void CanCompareRecordRequests()
        {
            var instance = new RecordRequest
            {
                Description = "description"
            };

            var instanceToCompareTo = new RecordRequest
            {
                Description = "description"
            };

            instance.ShouldEqual(instanceToCompareTo);
        }
    }
}

感谢任何帮助!

I'm trying to get up to speed with TDD so I've created a new S#arp Arch application (v1.6) and have been walking through creating tests for my classes. Unfortunately I've already run in to an issue I've yet to resolve.

I've created a comparison test for both a 'User' and 'RecordRequest' class. When executing the tests with NUnit the 'User' test passes whereas the 'RecordRequest' does not. I'm trying to understand why one test is passing and the other is not. The error message is:

at SharpArch.Testing.NUnit.SyntaxHelpers.ShouldEqual(Object actual, Object expected) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Testing.NUnit\SyntaxHelpers.cs:line 25
at Tests.ISPS.Core.Account.RecordRequestTests.CanCompareRecordRequests() in D:\Web\Mvc\Projects\Campus Counsel\IRS\ISPS\tests\ISPS.Tests\ISPS.Core\Account\RecordRequestTests.cs:line 23

'User' class and test:

using System;
using System.Security.Principal;
using NHibernate.Validator.Constraints;
using SharpArch.Core.DomainModel;

namespace ISPS.Core.Account
{
    public class User : Entity, IPrincipal
    {
        public static User DefaultUser()
        {
            return new User
            {
                Role = Role.Administrator,
                IsActive = true,
                CreatedOn = DateTime.Now
            };
        }

        [DomainSignature]
        [NotNullNotEmpty(Message = "Kerberos must be provided.")]
        public virtual string Kerberos { get; set; }

        [NotNullNotEmpty(Message = "First name must be provided.")]
        public virtual string FirstName { get; set; }

        [NotNullNotEmpty(Message = "Last name must be provided.")]
        public virtual string LastName { get; set; }

        [DomainSignature]
        [NotNullNotEmpty(Message = "Email must be provided.")]
        public virtual string Email { get; set; }

        [NotNull(Message = "Role must be provided.")]
        public virtual Role Role { get; set; }

        public virtual bool IsActive { get; set; }
        public virtual DateTime CreatedOn { get; set; }
        public virtual DateTime? ModifiedOn { get; set; }

        public virtual bool IsAdministrator { get { return Role.Id == Role.AdministratorId; } }
        public virtual bool IsEditor { get { return Role.Id == Role.EditorId; } }
        public virtual bool IsGuest { get { return Role.Id == Role.GuestId; } }

        public virtual IIdentity Identity
        {
            get
            {
                var isAuthenticated = (Role.Name == Role.Administrator.Name);
                return new Identity(isAuthenticated, Kerberos);
            }
        }

        public virtual bool IsInRole(string role)
        {
            return Role.Name == role;
        }
    }

    /// <summary>
    /// Simple implementation of IIdentity
    /// </summary>
    public class Identity : IIdentity
    {
        public Identity(bool isAuthenticated, string name)
        {
            IsAuthenticated = isAuthenticated;
            Name = name;
        }

        public string AuthenticationType
        {
            get { return "Forms"; }
        }

        public bool IsAuthenticated { get; private set; }

        public string Name { get; private set; }
    }
}

using System;
using NUnit.Framework;
using SharpArch.Testing.NUnit;
using ISPS.Core.Account;

namespace Tests.ISPS.Core.Account
{
    [TestFixture]
    public class UserTests
    {
        [Test]
        public void CanCompareUsers()
        {
            var instance = new User
                               {
                                   Kerberos = "nchomsky",
                                   FirstName = "Noam",
                                   LastName = "Chomsky",
                                   Email = "[email protected]",
                                   Role = Role.Administrator,
                                   IsActive = true,
                                   CreatedOn = DateTime.Parse("1/1/1975 10:00 AM"),
                                   ModifiedOn = null
                               };

            var instanceToCompareTo = new User
                                          {
                                              Kerberos = "nchomsky",
                                              FirstName = "Noam",
                                              LastName = "Chomsky",
                                              Email = "[email protected]",
                                              Role = Role.Administrator,
                                              IsActive = true,
                                              CreatedOn = DateTime.Parse("1/1/1975 10:00 AM"),
                                              ModifiedOn = null
                                          };

            instance.ShouldEqual(instanceToCompareTo);
        }
    }
}

'RecordRequest' class and test:

using NHibernate.Validator.Constraints;
using SharpArch.Core.DomainModel;

namespace ISPS.Core.Requests
{
    public class RecordRequest : Entity
    {
        public static RecordRequest DefaultRecordRequest()
        {
            return new RecordRequest();
        }

        [NotNullNotEmpty(Message = "A description of the documents requested must be provided.")]
        public virtual string Description { get; set; }
    }
}

using ISPS.Core.Requests;
using NUnit.Framework;
using SharpArch.Testing.NUnit;

namespace Tests.ISPS.Core.Requests
{
    [TestFixture]
    public class RecordRequestTests
    {
        [Test]
        public void CanCompareRecordRequests()
        {
            var instance = new RecordRequest
            {
                Description = "description"
            };

            var instanceToCompareTo = new RecordRequest
            {
                Description = "description"
            };

            instance.ShouldEqual(instanceToCompareTo);
        }
    }
}

Any assistance is appreciated!

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

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

发布评论

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

评论(1

英雄似剑 2024-10-18 16:33:40

尝试在 Description 属性上添加 [DomainSignature] 属性。

Try adding [DomainSignature] attribute on Description property.

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