为什么AutoFixture无法使用StringLength数据注释? [英] Why isn't AutoFixture working with the StringLength data annotation?

查看:87
本文介绍了为什么AutoFixture无法使用StringLength数据注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次尝试升级到 AutoFixture 2,我遇到了问题我对象上的数据注释。这是一个示例对象:

 公共类Bleh 
{
[StringLength(255)]
。公共字符串Foo {get;组; }
公共字符串Bar {get;组; }
}

我正在尝试创建匿名 Bleh ,但带有注释的属性将显示为空,而不是使用匿名字符串填充。

  [测试] 
public void GetAll_HasContacts()
{
var fix = new Fixture();
var bleh = fix.CreateAnonymous< Bleh>();

Assert.That(bleh.Bar,Is.Not.Empty); //通过
Assert.That(bleh.Foo,Is.Not.Empty); //失败?!
}

根据奖励位, StringLength ,即使不支持$ c>,也不希望有一个空字符串。我正在使用 NuGet 中的v2.7.1。我是否错过了创建带有数据注释的对象所必需的某种自定义或行为?

解决方案

感谢报告! / p>

此行为是设计使然(其原因基本上是对属性本身的描述)。



通过在数据字段上应用[StringLength(255)],基本上意味着它可以包含0个字符,最多255个字符。



根据msdn上的描述,StringLengthAttribute类:





当前版本(2.7.1)基于.NET Framework 3.5。从2.4.0开始支持StringLengthAttribute类。



也就是说,创建的实例是有效的(只是第二个断言语句无效)



这里是通过测试,使用 Validator 来自System.ComponentModel.DataAnnotations命名空间的类:

  using System.Collections.Generic; 
使用System.ComponentModel.DataAnnotations;
使用NUnit.Framework;
使用Ploeh.AutoFixture;

公共类测试
{
[测试]
public void GetAll_HasContacts()
{
var Fixture = new Fixture();
var bleh = Fixture.CreateAnonymous< Bleh>();

var context = new ValidationContext(bleh,serviceProvider:null,items:null);

//用于保存每个失败的验证的集合。
var results = new List< ValidationResult>();

//如果对象通过验证,则返回true;否则,返回true。否则为假。
var success = Validator.TryValidateObject(bleh,context,
results,validateAllProperties:true);

断言。(成功的是。是); //通过
Assert.That(results,Is.Empty); //通过
}

公共类Bleh
{
[StringLength(255)]
公共字符串Foo {get;组; }
公共字符串Bar {get;组; }
}
}

更新1



虽然创建的实例是有效,但我认为可以对其进行调整以选择范围(0-maximumLength)内的随机数,因此用户永远不会得到一个空字符串。



我还在论坛此处创建了一个讨论区



更新2



原始测试用例将如果升级到AutoFixture 2.7.2版(或更高版本),则现在通过。

  [测试] 
public void GetAll_HasContacts ()
{
var fix = new Fixture();
var bleh = fix.CreateAnonymous< Bleh>();

Assert.That(bleh.Bar,Is.Not.Empty); //通过
Assert.That(bleh.Foo,Is.Not.Empty); //通过(版本2.7.2或更高版本)
}


I'm trying again to upgrade to AutoFixture 2, and I'm running into a problem with the data annotations on my objects. Here's an example object:

public class Bleh
{
    [StringLength(255)]
    public string Foo { get; set; }
    public string Bar { get; set; }
}

I'm attempting to create an anonymous Bleh, but the property with the annotation is coming up empty rather than being populated with an anonymous string.

[Test]
public void GetAll_HasContacts()
{
    var fix = new Fixture();
    var bleh = fix.CreateAnonymous<Bleh>();

    Assert.That(bleh.Bar, Is.Not.Empty);  // pass
    Assert.That(bleh.Foo, Is.Not.Empty);  // fail ?!
}

According to Bonus Bits, StringLength should be supported as of 2.4.0, though even if it wasn't supported I wouldn't expect an empty string. I am using v2.7.1 from NuGet. Have I missed some sort of customization or behavior that is necessary to create data-annotated objects?

解决方案

Thanks for reporting this!

This behavior is by design (the reason for that is, basically, the description of the attribute itself).

By applying [StringLength(255)] on a data field it basically means that it is allowed to have 0 up-to 255 characters.

According to the description on msdn, the StringLengthAttribute Class:

  • Specifies the maximum length of characters that are allowed in a data field. [.NET Framework 3.5]

  • Specifies the minimum and maximum length of characters that are allowed in a data field. [.NET Framework 4]

The current version (2.7.1) is built on .NET Framework 3.5. The StringLengthAttribute class is supported as of 2.4.0.

That being said, the created instance is valid (it's just that the second assertion statement isn't).

Here is a passing test that validates the created instance using the Validator class from the System.ComponentModel.DataAnnotations namespace:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using NUnit.Framework;
using Ploeh.AutoFixture;

public class Tests
{
    [Test]
    public void GetAll_HasContacts()
    {
        var fixture = new Fixture();
        var bleh = fixture.CreateAnonymous<Bleh>();

        var context = new ValidationContext(bleh, serviceProvider: null, items: null);

        // A collection to hold each failed validation.
        var results = new List<ValidationResult>();

        // Returns true if the object validates; otherwise, false.
        var succeed = Validator.TryValidateObject(bleh, context, 
            results, validateAllProperties: true);

        Assert.That(succeed, Is.True);  // pass
        Assert.That(results, Is.Empty); // pass
    }

    public class Bleh
    {
        [StringLength(255)]
        public string Foo { get; set; }
        public string Bar { get; set; }
    }
}

Update 1:

While the created instance is valid, I believe that this could be adjusted to pick a random number inside the range (0 - maximumLength) so the user never gets an empty string.

I have also created a discussion at the forum here.

Update 2:

The original test case will now pass if you upgrade to AutoFixture version 2.7.2 (or newer).

[Test]
public void GetAll_HasContacts()
{
    var fix = new Fixture();
    var bleh = fix.CreateAnonymous<Bleh>();

    Assert.That(bleh.Bar, Is.Not.Empty);  // pass
    Assert.That(bleh.Foo, Is.Not.Empty);  // pass (version 2.7.2 or newer)
}

这篇关于为什么AutoFixture无法使用StringLength数据注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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