简体   繁体   English

AutoFixture:如何正确使用 ISpecimenBuilder 实现中的 BooleanSwitch() 来生成随机值?

[英]AutoFixture: How to properly use BooleanSwitch() from an ISpecimenBuilder implementation to generate random values?

I have a class that consists entirely of string types... I am not happy with this, but cant change it.我有一个完全由字符串类型组成的类......我对此不满意,但无法更改它。 This class is the representation of some CSVs that are getting parsed.这个类是一些正在解析的 CSV 的表示。

Now I would like to generate fake instances.现在我想生成假实例。 For example I would like to generate randomized boolean values and convert them to a string.例如,我想生成随机布尔值并将它们转换为字符串。 I have therefor created an implementation of ISpecimenBuilder which works so far.因此,我创建了一个ISpecimenBuilder的实现,它目前有效。

public class MyPropertyBuilder : ISpecimenBuilder
{
    private readonly Random rand;

    public MyPropertyBuilder()
    {
        this.rand = new Random();
    }
    public object Create(object request, ISpecimenContext context)
    {
        var pi = request as PropertyInfo;

        if (pi == null)
        {
            return new NoSpecimen();
        }

        if (pi.PropertyType == typeof(string) && pi.Name == "MyPropertyName")
        {
            return (this.rand.NextDouble() > 0.5).ToString();
        }

        return new NoSpecimen();
    }
}

But I somehow don't understand on how to properly use context.Resolve() and the *Request classes like RangedNumberRequest() as in the following code snippet.但我不知何故不明白如何正确使用context.Resolve()*Request类,如RangedNumberRequest()如下面的代码片段所示。

public class UnsignedIntegerNumberBuilder : ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        var pi = request as PropertyInfo;

        if (pi == null)
        {
            return new NoSpecimen();
        }

        if (pi.PropertyType == typeof(string) && pi.Name == "ORDER_NR")
        {
            return context.Resolve(new RangedNumberRequest(typeof(int), 0, int.MaxValue)).ToString();
        }

        return new NoSpecimen();
    }
}

Of course I can implement my own way to generate a random boolean value and make MyPropertyBuilder return that, but doesnt that defeat the purpose of AutoFixture as I somehow reinvent the data generation part for some primitve types?当然,我可以实现自己的方式来生成随机布尔值并使MyPropertyBuilder返回该值,但这并不会违背 AutoFixture 的目的,因为我以某种方式为某些原始类型重新发明了数据生成部分?

So I guess the question is: How can I properly use AutoFixtures boolean value generator for a specific property?所以我想问题是:如何为特定属性正确使用 AutoFixtures 布尔值生成器?

If I understand correctly all properties of the class are of type string , but represent other data types (boolean, integer or dates).如果我理解正确,该类的所有属性都是string类型,但代表其他数据类型(布尔值、整数或日期)。

My suggestion would be to create a FixtureBuilder class with property types you expects to be.我的建议是使用您期望的属性类型创建一个 FixtureBuilder 类。 With Autfixture you will be able to generate random values and then convert it to the csv representation.使用 Autfixture,您将能够生成随机值,然后将其转换为 csv 表示。

public class CsvData
{
    public string Enabled { get; set; }
    public string Quantity { get; set; }
    public string Price { get; set; }
}

public class CsvDataBuilder
{
    public bool Enabled { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; } 

    public CsvData ToCsvData()
    {
        return new CsvData
        {
            Enabled = Enabled.ToString(),
            Quantity = Quantity.ToString(),
            Price = Price.ToString()
        };
    }
}

Then in the tests you can create builder within然后在测试中,您可以在其中创建构建器

var dataFromCsv = new Fixture().Create<CsvDataBuilder>().ToCsvData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM