6

データフィールド値を使用してテキストブロックをレンダリングするアプリケーションをテストしたいと思います。レンダリングが完了したら、実際の幅と実際の高さを取得したいと思います。すべてが正常に動作します。アプリケーションをテストしようとしたときに、問題が最初に発生しました。テストプロジェクトからディスパッチャを呼び出すことができません。

以下はコードです。

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を使い始めたばかりです。だから、私を助けてください。

ありがとう

4

2 に答える 2

2

http://www.codeproject.com/KB/WPF/UnitTestDispatcherTimer.aspxを参照する ことをお勧めします。これは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);
于 2010-03-16T06:34:56.237 に答える
1

これまでnUnitを使用して単体テストを作成したことはありませんが、これはVS単体テストでよくある問題です。最終的に発生する可能性があるのは、各テストで異なるディスパッチャーが使用され、WPFでは同じディスパッチャーを使用する必要があるということです。これを回避するには、静的クラスを作成してDispatcherをキャッシュし、それを介してすべてを呼び出します。

于 2010-06-10T05:24:12.553 に答える