AutoFixture约束字符串参数 [英] AutoFixture constrained string parameter

查看:53
本文介绍了AutoFixture约束字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以为参数orderBy指定可能值的列表?请不要一一列举,否则我将不会提出这个问题.我想指定orderby仅在从预定列表中选择时才有意义.假设列表很大...仍然不是随机的.这可以不那么困难...没有一个简单的例子可以说明这个简单的任务.

Is there a simple way to specify a list of possible values for the parameter orderBy? Not one by one please, otherwise I would not be making the question. I want to specify that orderby makes sense only if it is chosen from a predetermined list. Suppose the list is very large...still not random. This cannot be that hard...no single example of such a simple task.

[Test, AutoData]
public override void IndexReturnsView(int? pageIndex, int? pageSize, string orderBy, bool? desc)
{
    .....
}

我想要做的就是像使用ValueSource属性那样从列表中读取可能的值.但是,它似乎不适用于AutoFixture.如果我指定例如 [ValueSource("GetOrderByColumnNames")] 我的测试不再起作用.我不知道我在做什么错.不幸的是,AutoFixture缺少有用的文档,并且示例非常基础.有没有这种情况的可行示例,可以在这里用来指导自己?

All I want is to read the possible values from a list as I would do with the ValueSource attribute. However, it seems not to work with AutoFixture. If I specified e.g. [ValueSource("GetOrderByColumnNames")] my test does not work anymore. I have no idea of what I am doing wrong. Unfortunately AutoFixture lacks useful documentation and the examples are very basic. Is there a working example of this scenario that I can use to guide myself here?

这肯定是非常普遍的情况,但是我一直在寻找没有运气的日子:(.

This has to be a very common situation, however I have been looking for days with no luck :(.

感激!

推荐答案

如果我正确理解了这个问题,那么问题是应该从预定义值的列表中随机选择 orderBy 值,但是该列表可能太大,无法与 [InlineAutoData] 一起使用.

If I understand the question correctly, the problem is that the orderBy value should be randomly selected from a list of predefined values, but that list might be too large to use with [InlineAutoData].

我能想到的最简单的方法是引入一个辅助类型.这实际上可能是对该应用程序的宝贵补充代码本身,因为它使各种值的作用更加明确,但是如果没有,则可以随时将包装器类型添加到测试代码库中.

The easiest way to do this that I can think of is to introduce a helper type. This might actually be a valuable addition to the application code itself, as it makes the role of various values more explicit, but if not, you can always add the wrapper type to the test code base.

这样的事情是您所需要的最低要求:

Something like this is the minimum you'll need:

public class OrderCriterion
{
    public OrderCriterion(string value)
    {
        Value = value;
    }

    public string Value { get; }
}

如果我们还想像该类公开了 ValidValues 的列表,则可以使用 ElementsBuilder 类实现AutoFixture定制:

If we also imagine that this class exposes a list of ValidValues, you can implement an AutoFixture Customization using the ElementsBuilder class:

public class OrderCriterionCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customizations.Add(
            new ElementsBuilder<OrderCriterion>(OrderCriterion.ValidValues));
    }
}

然后,为测试代码库创建一个数据源属性:

Then you create a data source attribute for your test code base:

public class TestConventionsAttribute : AutoDataAttribute
{
    public TestConventionsAttribute() : base(
        () => new Fixture().Customize(new OrderCriterionCustomization()))
    {
    }
}

这使您可以编写这样的测试,并通过:

This enables you to write a test like this, which passes:

[Theory, TestConventions]
public void IndexReturnsView(
    int? pageIndex,
    int? pageSize,
    OrderCriterion orderBy,
    bool? desc)
{
    Assert.Contains(orderBy.Value, OrderCriterion.ValidValues.Select(x => x.Value));
}

请注意,您无需将 orderBy 参数声明为 string ,而是将其声明为 OrderCriterion ,这意味着将检测到AutoFixture它的存在,然后进行定制.

Notice that instead of declaring the orderBy parameter as a string, you declare it as an OrderCriterion, which means that AutoFixture will be detect its presence, and the Customization then kicks in.

另请参见 https://stackoverflow.com/a/48903199/126014

这篇关于AutoFixture约束字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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