문제

데이터 필드 값으로 텍스트 블록을 렌더링하는 응용 프로그램을 테스트하고 싶습니다. 렌더링이 완료되면 실제 너비와 실제 높이를 얻고 싶습니다. 모든 것이 잘 작동합니다. 응용 프로그램을 테스트하려고 할 때 문제가 먼저 나왔습니다. 테스트 프로젝트에서 디스패처를 호출 할 수 없습니다.

다음은 코드입니다.

this.Loaded += (s, e) =>
{
    TextBlock textBlock1 = new TextBlock();

    //// Text block value is assigned from data base field.
    textBlock1.Text = strValueFromDataBaseField;        
    //// Setting the wrap behavior.
    textBlock1.TextWrapping = TextWrapping.WrapWithOverflow;
    //// Adding the text block to the layout canvas.
    this.layoutCanvas.Children.Add(textBlock1);

    this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
        (Action)(() =>
            {
                //// After rendering the text block with the data base field value. Measuring the actual width and height.
               this.TextBlockActualWidth = textBlock1.ActualWidth;
               this.TextBlockActualHeight = textBlock1.ActualHeight;

               //// Other calculations based on the actual widht and actual height.
            }
        ));
};

방금 Nunit을 사용하기 시작했습니다. 그러니 도와주세요.

감사

도움이 되었습니까?

해결책

나는 Nunit을 사용하여 이전에 단위 테스트를 작성하지 않았지만 VS 단위 테스트의 일반적인 문제입니다. 끝날 수있는 것은 각 테스트가 다른 디스패처를 사용하고 WPF는 동일한 디스패처를 사용해야한다는 것입니다. 이 문제를 해결하려면 정적 클래스를 만들어 디스패처를 캐시한 다음 모든 것을 통해 호출하십시오.

다른 팁

당신은보고 싶을 수도 있습니다 http://www.codeproject.com/kb/wpf/unittestdispatchertimer.aspx그것은 a DispatcherTimer WPF와 Nunit에서는 다음과 같이 사용합니다 Dispatcher.

편집하다

링크에서 테스트 전에 다음을 시도하고 수행하십시오.

Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, testMethod);
// Start the worker thread's message pump
Dispatcher.Run();  // This will block until the dispatcher is shutdown

테스트 후 중지하십시오.

Dispatcher disp = Dispatcher.CurrentDispatcher;
// Kill the worker thread's Dispatcher so that the
// message pump is shut down and the thread can die.
disp.BeginInvokeShutdown(DispatcherPriority.Normal);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top