Testy nie zawsze dają takie same wyniki. Od czasu do czasu wyjątek powoduje niepowodzenie testu. Zastanawiam się co robię nie tak.
Często korzystam z aplikacji, z której pochodzi ten kod. Aplikacja nigdy nie wysypał się na takim lub podobnym wyjątku związanym z Freezable.
Proszę o pomoc.
[Fact]
public void ShouldActivateWithEntryThatIsLastAndTimestampIsGreaterThanThatEntryDisappearAt()
{
Mock<IEventAggregator> eventAggregator = new();
var vm = new SelectionViewModel(eventAggregator.Object, new SubtitlesEntryFinder(new TestStore()));
var windowParams = new WindowParameters();
windowParams.Add("time", new TimeSpan(0, 1, 58));
//Act
vm.OnWindowActivated(windowParams);
vm.SearchPattern = "1";
vm.ConfirmWordsSelection();
//Assert
eventAggregator.Verify(
x => x.SendMessage(It.Is<AddSelectedWordsMessage>(x => x.words.SequenceEqual(new List<string> { "Third" }))),
Times.Once);
}
Message:
System.InvalidCastException : Unable to cast object of type 'MS.Utility.FrugalObjectList`1[System.Windows.Freezable+FreezableContextPair]' to type 'System.Windows.DependencyObject'.
Stack Trace:
Freezable.get_SingletonContext()
Freezable.ConvertToContextList()
Freezable.AddContextInformation(DependencyObject context, DependencyProperty property)
Freezable.AddInheritanceContext(DependencyObject context, DependencyProperty property)
DependencyObject.ProvideSelfAsInheritanceContext(DependencyObject doValue, 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.SetValue(DependencyProperty dp, Object value)
EntryDocumentCreator.CreateParagraph(List`1 groupOfEntryContent, Int32& number) line 46
EntryDocumentCreator.Create(DisplayEntry displayEntry) line 23
SelectionViewModel.OnWindowActivated(WindowParameters parameters) line 317
SelectionTests.ShouldActivateWithEntryThatIsLastAndTimestampIsGreaterThanThatEntryDisappearAt() line 67
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
Metoda, w której występuje problem.
private static Paragraph CreateParagraph(List<EntryContent> groupOfEntryContent, ref int number)
{
var textFontSize = 16D;
var subscriptFontSize = textFontSize - 4D;
var inlines = new List<Inline>();
var whitespace = "";
foreach (var entryContent in groupOfEntryContent)
{
foreach (var word in entryContent.Words)
{
number++;
var wordRun = new Run($"{whitespace}{word}");
wordRun.FontSize = textFontSize;
wordRun.Foreground = Globals.Brushes.NormalText; // linia 46
inlines.Add(wordRun);
var numberRun = new Run(number.ToString())
{
FontSize = subscriptFontSize,
Foreground = Globals.Brushes.WordNumber,
BaselineAlignment = BaselineAlignment.Subscript
};
inlines.Add(numberRun);
whitespace = " ";
}
}
var paragraph = new Paragraph();
paragraph.Inlines.AddRange(inlines);
return paragraph;
}
public static class Brushes
{
public static Brush WordNumber { get; } = new SolidColorBrush(Color.FromRgb(117, 117, 117));
public static Brush NormalText { get; } = new SolidColorBrush(Color.FromRgb(0, 0, 0));
public static Brush HighlightedText { get; } = new SolidColorBrush(Color.FromRgb(229, 107, 0));
}