c# - 如何在 Nunit 中调用 WPF Dispatcher?

标签 c# wpf nunit dispatcher nunit-mocks

我想测试一个呈现带有数据字段值的文本 block 的应用程序。渲染完成后,我想获得实际宽度和实际高度。一切正常。当我尝试测试应用程序时,问题首先出现。我无法从测试项目调用调度程序。

代码如下。

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。所以,请帮助我。

谢谢

最佳答案

您可能想看看 http://www.codeproject.com/KB/WPF/UnitTestDispatcherTimer.aspx 它处理 WPF 和 NUnit 中的 DispatcherTimer,后者又使用 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);

关于c# - 如何在 Nunit 中调用 WPF Dispatcher?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2452246/

相关文章:

c# - 我如何获得已售出的成员(member)数量

c# - 系统.Windows.数据错误 : 4 : Cannot find source for binding with reference

attributes - Nunit Action 属性构造函数被多次调用

unit-testing - 单元测试后 Controller .NET Web Api

c# - 分层 "OneTimeSetUp"方法

c# - 如何将 WebResponse.GetResponseStream 返回值转换成字符串?

c# - MSTest - 带有列表参数的最小起订量设置返回空列表

c# - 在销毁最后一个克隆 c# 后,如何加载下一个级别

c# - 如何在 wpf 中使两个段落 block 在同一级别上,在级别上,保持一个 block 左对齐,另一个右对齐?

c# - 如何隐藏 WPF 元素以与自动化框架一起正常工作?