我有很多Xunit测试都可以正常工作,但是有时调用下面列出的代码会失败。尽管这些测试不一定共享相同的受影响的代码,但它们却一起失败。如果我再次运行它们,它们很可能全部通过,并且在几次测试运行中也不会再看到错误。
所有测试均失败,并显示类似以下内容:
Message:
System.IndexOutOfRangeException : Index was outside the bounds of the array.
Stack Trace:
ArrayItemList`1.EntryAt(Int32 index)
FrugalObjectList`1.get_Item(Int32 index)
Freezable.AddContextToList(DependencyObject context, DependencyProperty property)
Freezable.AddContextInformation(DependencyObject context, DependencyProperty property)
Freezable.AddInheritanceContext(DependencyObject context, DependencyProperty property)
DependencyObject.ProvideSelfAsInheritanceContext(DependencyObject doValue, DependencyProperty dp)
DependencyObject.ProvideSelfAsInheritanceContext(Object value, DependencyProperty dp)
DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
Pen.ctor(Brush brush, Double thickness)
CanvasPathDrawer.CreateOutlinePen() line 194
失败的代码是
private Pen CreateOutlinePen()
{
var outlinePenBrush = CreateOutlinePenBrush(OutlineType.None);
var outlinePen = new Pen(outlinePenBrush, 1) // Fails on this line
{
EndLineCap = PenLineCap.Round,
DashCap = PenLineCap.Round,
LineJoin = PenLineJoin.Round,
StartLineCap = PenLineCap.Round,
MiterLimit = 10.0
};
return outlinePen;
}
private static Brush CreateOutlinePenBrush(OutlineType outlineType)
{
switch (outlineType)
{
case OutlineType.None:
return OutlineTypeNoneBrush;
// etc
}
}
public static Brush OutlineTypeNoneBrush { get; } = new SolidColorBrush(Colors.Transparent);
使用[StaFact]属性在STA线程上运行测试。
我想我已经错过了有关如何创建对象的基本知识。我是否应该每次都创建一个新画笔?