您将如何对测试数据注释进行单元化? [英] How would you unit test data annotations?

查看:70
本文介绍了您将如何对测试数据注释进行单元化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个类属性具有以下注释:

Two of the class properties have the following annotations:

    [Key]
    [Column]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }


    [MaxLength(25)]
    public string Name { get; set; }

我知道测试Key,Column和Required属性不再是单元测试,而是一种集成进行测试,因为它取决于基础数据库,但是如何测试MaxLength(25)属性?

I understand that testing Key, Column and Required attributes is no longer a unit test, it's an integration test as it would depend on the underlying database, but how do you go about testing MaxLength(25) attribute?

我能想到的替代方法之一是

One of the alternatives that I can think of, is to add a code contract into the property.

更新

我建议编写了以下帮助程序:

As suggested, I wrote the following helper:

    public class AttributeHelper <T> where T : class
    {
        private Type GivenClass 
        { 
            get { return typeof (T); }
        }

        public bool HasAnnotation(Type annotation)
        {
            return GivenClass.GetCustomAttributes(annotation, true).Single() != null;
        }

        public bool MethodHasAttribute(Type attribute, string target)
        {
           return GivenClass.GetMethod(target).GetCustomAttributes(attribute, true).Count() == 1;
        }

        public bool PropertyHasAttribute(Type attribute, string target)
        {
            return GivenClass.GetProperty(target).GetCustomAttributes(attribute, true).Count() == 1;
        }

    }

我然后测试了我的助手:

I have then tested my helper:

    [TestMethod]
    public void ThisMethod_Has_TestMethod_Attribute()
    {
        // Arrange
        var helper = new AttributeHelper<AttributeHelperTests>();

        // Act
        var result = helper.MethodHasAttribute(typeof (TestMethodAttribute), "ThisMethod_Has_TestMethod_Attribute");

        // Assert
        Assert.IsTrue(result);
    }

除方法和属性必须在公共范围内公开外,其他所有方法都可以正常工作让我使用反射。我想不出必须将属性添加到私有属性/方法的任何情况。

Everything works fine, apart from the fact that methods and properties must be public in order for me to use reflection. I can't think of any cases where I had to add attributes to the private properties/methods.

然后测试EF注释:

        public void IdProperty_Has_KeyAttribute()
        {
            // Arrange
            var helper = new AttributeHelper<Player>();

            // Act
            var result = helper.PropertyHasAttribute(typeof (KeyAttribute), "Id");

            // Assert
            Assert.IsTrue(result);
        }


推荐答案


我了解测试键,列和必填属性不再是单元测试,而是集成测试,因为它取决于基础数据库

I understand that testing Key, Column and Required attributes is no longer a unit test, it's an integration test as it would depend on the underlying database

那是怎么回事?您可以测试 Id 属性是否标记有所有这些属性。

How is that so? You can test whether Id property is marked with all those attributes just fine. And it falls into unit-test category.

[Test]
public void Id_IsMarkedWithKeyAttribute()
{
    var propertyInfo = typeof(MyClass).GetProperty("Id");

    var attribute = propertyInfo.GetCustomAttributes(typeof(KeyAttribute), true)
        .Cast<KeyAttribute>()
        .FirstOrDefault();

    Assert.That(attribute, Is.Not.Null);
}

这样,您可以确保属性标记有您能想到的任何属性。当然,这涉及到一些反思工作,但这就是测试属性标记的方法。

This way you can assure your properties are marked with any attribute you can think of. Sure, this involves some reflection work but that's how you test attribute marking.

这篇关于您将如何对测试数据注释进行单元化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆