단위 테스트 가능으로하고 싶은 MVVM-Lite 애플리케이션이 있습니다. 모델은 System.Timers.Timer를 사용하므로 update 이벤트는 백그라운드 작업자 스레드에서 끝납니다. 이 단위 테스트를 거쳤지 만 런타임에 System.NotSupportedException이 발생했습니다. "이 유형의 CollectionView는 Dispatcher 스레드와 다른 스레드에서 해당 SourceCollection에 대한 변경을 지원하지 않습니다." MVVM-lite 클래스 Threading.DispatcherHelper가 문제를 해결할 것이라고 기대했지만 DispatcherHelper.CheckBeginInvokeOnUI를 호출하면 유닛 테스트가 실패합니다. 여기에 내가 http://kentb.blogspot.com/2009/04/mvvm-infrastructure-viewmodel.html의 트릭하지만 Dispatcher.CurrentDispatcher에 호출 할 수있는 전화를 시도했다 뷰 모델단위 테스트 할 수있는 MVVM 코드에서 Dispatcher 사용
private void locationChangedHandler(object src, LocationChangedEventArgs e)
{
if (e.LocationName != this.CurrentPlaceName)
{
this.CurrentPlaceName = e.LocationName;
List<FileInfo> filesTaggedForHere = Tagger.FilesWithTag(this.CurrentPlaceName);
//This nextline fixes the threading error, but breaks it for unit tests
//GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(delegate { updateFilesIntendedForHere(filesTaggedForHere); });
if (Application.Current != null)
{
this.dispatcher.Invoke(new Action(delegate { updateFilesIntendedForHere(filesTaggedForHere); }));
}
else
{
updateFilesIntendedForHere(filesTaggedForHere);
}
}
}
private void updateFilesIntendedForHere(List<FileInfo> filesTaggedForHereIn)
{
this.FilesIntendedForHere.Clear();
foreach (FileInfo file in filesTaggedForHereIn)
{
if (!this.FilesIntendedForHere.Contains(file))
{
this.FilesIntendedForHere.Add(file);
}
}
}
에서 함께 결국 한 코드 것은 단위 테스트 중에 실행하지 못했습니다 때문에 실패했습니다. 그래서 실행이 응용 프로그램이 아닌 테스트에 있다면 헬퍼 메서드를 직접 호출하는 것입니다.
이것은 옳지 않을 수 있습니다. ViewModel은 어디에서 호출되는지 상관하지 않아야합니다. 아무도 왜 켄트 Boogaart의 발송자 방법도 MVVM - 라이트 DispatcherHelper.CheckBeginInvokeOnUI 내 단위 테스트에서 작동 볼 수 있습니까?