4

我正在开发一个 ASP.NET MVC 2 应用程序,我像这样测试我的 Viewmodels

[TestMethod]
public void EmailNeedsToBeSet()
{
    var pvm = new ProfileViewModel {Email = ""};
    var validationResults = new ValidationResults();
    var result = Validator.TryValidateObject(pvm, new ValidationContext(pvm, null, null), validationResults, true);
    Assert.IsFalse(result);
    Assert.IsTrue(validationResults.ContainsField<ProfileViewModel>(m => m.Email));
}

电子邮件属性是必需的,此测试方法工作正常

我现在的问题是:如何像之前的测试一样测试 MetaData 类?验证结果总是正确的。或者这不可能吗?

我的元数据类示例:

[MetadataType(typeof (UserMetaData))]
public partial class User
{
   public class UserMetaData
   {
      [Required(ErrorMessageResourceName = "Required",
      ErrorMessageResourceType = typeof (ValidationStrings))]
      [LocalizedDisplayName("LoginName", NameResourceType = typeof (Fields))]
      [StringLength(64)]
      public string Login { get; set; }
   }
}
4

1 回答 1

2

以下是我如何对这种类型的代码进行单元测试:

    [TestMethod]
    public void IsValid_Returns_ValidationResult_Errors_When_Properties_With_Meta_Data_Are_Not_Valid()
    {
        var sut = new User
                      {
                          Login = "too long!!!"
                      };

        var actual = sut.IsValid();

        Assert.IsNotNull(actual);
        Assert.AreEqual(1, actual.Count);
        Assert.AreEqual(1, actual[0].MemberNames.Count());
        var firstValidationError = actual[0];
        Assert.AreEqual("Login", firstValidationError.MemberNames.First());
        Assert.AreEqual("The field Login must be a string with a maximum length of 4.", firstValidationError.ErrorMessage);
    }

    [TestMethod]
    public void IsValid_Returns_Empty_List_Of_Errors_When_Properties_With_Meta_Data_Are_Valid()
    {
        var sut = new User
                      {
                          Login = "abcd"
                      };

        var actual = sut.IsValid();

        Assert.IsNotNull(actual);
        Assert.AreEqual(0, actual.Count);
    }

[MetadataType(typeof(UserMetaData))]
public partial class User : BaseEntity
{
    public string Login { get; set; }

    public class UserMetaData
    {
        [Required]              
        [StringLength(4)]       
        public string Login { get; set; }
    }
}

public abstract class BaseEntity
{
    protected BaseEntity()
    {
        RegisterMetadata();
    }

    private void RegisterMetadata()
    {
        var currentType = GetType();

        var metadataType = GetMetadataType();

        if (metadataType == null)
            return;

        var descriptorProvider = new AssociatedMetadataTypeTypeDescriptionProvider(currentType, metadataType);
        TypeDescriptor.AddProviderTransparent(descriptorProvider, currentType);
    }

    private Type GetMetadataType()
    {
        var attribute = Attribute.GetCustomAttributes(this.GetType()).ToList().Find(t => t is MetadataTypeAttribute);

        return (attribute != null)
                   ? ((MetadataTypeAttribute)attribute).MetadataClassType
                   : null;
    }

    public virtual List<ValidationResult> IsValid()
    {
        var validationErrors = new List<ValidationResult>();
        var context = new ValidationContext(this, null, null);
        Validator.TryValidateObject(this, context, validationErrors, true);

        return validationErrors;
    }
}
于 2011-08-16T20:28:26.623 回答