考虑以下模型
public class CalculatedValue
{
//...
public ItemsChoiceType[] ItemsElementName { get; set; }
}
public enum ItemsChoiceType
{
Value1,
Value2,
//...
ValueN
}
以下代码片段将确保每个枚举值至少出现一次吗?如果没有,哪种方法更好(更明确)?我的enum
包含200多个元素...
var fixture = new Fixture();
var fake = fixture.Create<CalculatedValue>();
var itemsChoiceTypeElements = Enum.GetNames(typeof(ItemsChoiceType)).Length;
fake.ItemsElementName = fixture.CreateMany<ItemsChoiceType>(itemsChoiceTypeElements).ToArray();
答案 0 :(得分:0)
我会这样使用Except()LINQ函数:
class Program
{
enum Days
{
Monday,
Tuesday,
Wednesday
}
static void Main(string[] args)
{
Days[] allDays = { Days.Monday, Days.Tuesday, Days.Wednesday };
// This prints true cause all enum days exist in allDays
Debug.WriteLine(Enum.GetValues(typeof(Days)).OfType<Days>().Except(allDays).Count() == 0);
Days[] someDays = { Days.Monday, Days.Wednesday };
// This prints false cause not all days exist in someDays
Debug.WriteLine(Enum.GetValues(typeof(Days)).OfType<Days>().Except(someDays).Count() == 0);
}
}
答案 1 :(得分:0)
var calculatedValue = new Fixture().Create<Generator<CalculatedValue>>();
fixture.Customize<CalculatedValue>(c => c.Without(x => x.ItemsElementName));
fixture.Customize<IEnumerable<CalculatedValue>>(
c => c.FromFactory(
() => Enum.GetValues(typeof(ItemsChoiceType)).Cast<ItemsChoiceType>()
.Select(s => calculatedValue.First(u => u.ItemsElementName == s))));
var calculatedValues = fixture.Create<IEnumerable<CalculatedValue>>();