【问题标题】:Can AutoFixture create a list of objects for each possible enum that is a property of that class?AutoFixture 可以为作为该类属性的每个可能的枚举创建一个对象列表吗?
【发布时间】:2020-09-06 21:39:13
【问题描述】:

我是 Autofixture 的新手,但我非常喜欢它提供的功能。

我有一个类可以处理不同类型的推送通知,对于每种可能的类型,我都有一个 Enum。 我希望 Autofixture 为每种枚举类型创建一个对象,但还不能这样做。

我的简单通知类如下所示:

public class PushNotificationModel
    {
        public string CustomerId { get; set; }
        public EPushNotifications NotificationType { get; set; }
        public bool EnableNotification { get; set; }
    }

使用枚举:

public enum EPushNotifications
    {
        DailyConsumption,
        WeeklyConsumption,
        MonthlyConsumption,
        QuarterlyConsumption,
        Marketing
    }

之后我使用 XUnit 进行断言。

非常感谢任何帮助! :)

【问题讨论】:

    标签: xunit autofixture


    【解决方案1】:

    你可以枚举一个枚举

    var builder = new Fixture().Build<PushNotificationModel>();
    
    var allTypes = Enum.GetValues(typeof(EPushNotifications))
       .Select(value => (EPushNotifications)value)
       .Select(type => builder.With(e => NotificationType, type).Create())
       .ToArray();
    

    【讨论】:

    • 非常感谢!这正是我想要的! :) 祝您有美好的一天,感谢您抽出宝贵时间发帖。
    猜你喜欢
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多