AutoFixture可以为每个可能的枚举类创建一个对象列表吗?

问题描述 投票:0回答:1

我是Autofixture的新手,但我非常喜欢它所提供的功能,我有一个处理不同类型的Push通知的类,对于每个可能的类型,我都有一个Enum。

我有一个处理不同类型的Push通知的类,对于每一种可能的类型,我都有一个Enum.我想让Autofixture为每一个enum类型创建一个对象,但是还不能做到。

我的简单通知类是这样的。

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

有了enum:

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

我用XUnit来做后面的断言。

任何帮助都是非常感激的 :)

xunit autofixture
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.