Search code examples
xunitautofixture

Can AutoFixture create a list of objects for each possible enum that is a property of that class?


I'm new to Autofixture but I very much enjoy the features it provides.

I have a class that handles different kinds of Push notifications, for each type possible I have an Enum. I want Autofixture to create one object of each enum type, but haven't been able to do so yet.

My simple notification class looks like this:

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

With the enum:

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

I'm using XUnit to do the assertion afterwards.

Any help is greatly appreciated! :)


Solution

  • You can enumerate an enum

    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();