S#arp Arch and TDD
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>开发者_Python百科
/// 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 = "nchomsky@gmail.edu",
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 = "nchomsky@gmail.edu",
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!
Try adding [DomainSignature] attribute on Description property.
精彩评论