使用 AutoFixture 为字符串属性生成匿名号码

发布于 2025-01-04 02:50:00 字数 130 浏览 1 评论 0原文

我正在对一些映射方法进行单元测试,并且我有一个字符串类型的源属性,该属性映射到整数类型的目标属性。

因此,我希望 AutoFixture 使用匿名整数为特定字符串属性(而不是所有字符串属性)创建源对象。

这可能吗?

I'm unit testing some mapping methods, and I have a source property of type string which is mapped to a destination property of type integer.

So I would like AutoFixture to create the source object with an anonymous integer for the specific string property, not for all string properties.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恰似旧人归 2025-01-11 02:50:00

解决此问题的最佳方法是创建一个基于约定的自定义值生成器< /a> 将匿名数值的字符串表示形式分配给特定属性,基于其名称

因此,举个例子,假设您有一个如下所示的类:

public class Foo
{
    public string StringThatReallyIsANumber { get; set; }
}

自定义值生成器将如下所示:

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

        if (targetProperty == null)
        {
            return new NoSpecimen(request);
        }

        if (targetProperty.Name != "StringThatReallyIsANumber")
        {
            return new NoSpecimen(request);
        }

        var value = context.CreateAnonymous<int>();

        return value.ToString();
    }
}

这里的关键点是自定义生成器将仅针对名为 StringThatReallyIsANumber 的属性,在此case 是我们的惯例

为了在测试中使用它,您只需通过 Fixture.Customizations 集合将其添加到您的 Fixture 实例即可:

var fixture = new Fixture();
fixture.Customizations.Add(new StringThatReallyIsANumberGenerator());

var anonymousFoo = fixture.CreateAnonymous<Foo>();

The best way to solve this would be to create a convention based custom value generator that assigns the string representation of an anonymous numeric value to the specific property, based on its name.

So, to give an example, assuming you have a class like this:

public class Foo
{
    public string StringThatReallyIsANumber { get; set; }
}

The custom value generator would look like this:

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

        if (targetProperty == null)
        {
            return new NoSpecimen(request);
        }

        if (targetProperty.Name != "StringThatReallyIsANumber")
        {
            return new NoSpecimen(request);
        }

        var value = context.CreateAnonymous<int>();

        return value.ToString();
    }
}

The key point here is that the custom generator will only target properties named StringThatReallyIsANumber, which in this case is our convention.

In order to use it in your tests, you will simply have to add it to your Fixture instance through the Fixture.Customizations collection:

var fixture = new Fixture();
fixture.Customizations.Add(new StringThatReallyIsANumberGenerator());

var anonymousFoo = fixture.CreateAnonymous<Foo>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文