【问题标题】:WPF project - unable to set up unit testing (C#)WPF 项目 - 无法设置单元测试 (C#)
【发布时间】:2018-07-05 20:11:41
【问题描述】:

我正在尝试为我拥有的 WPF 项目设置单元测试。我在一个名为 WPFTester 的项目中有一个名为 MoveSelectionOutOfSelectedBox 的公共方法。方法如下……

  public void MoveSelectionOutOfSelectedBox(object sender, RoutedEventArgs e)
    {
        if (testSelectedBox.Items.Count == 1 && testSelectedBox.SelectedItem == null)
        {
            testSelectedBox.Items.Clear();
            testSelectedBox2.Items.Clear();
            spOptionsAcceptableRangeMax.Visibility = spOptionsAcceptableRangeMin.Visibility = spOptionsLabel.Visibility = spOptionsValue.Visibility = Visibility.Hidden;
            xmlData2.Clear();
        }
        else
        {
            xmlData2.RemoveAt(testSelectedBox.SelectedIndex);
            testSelectedBox.Items.RemoveAt(testSelectedBox.SelectedIndex);

            if (testSelectedBox.Items.Count == 0)
            {
                spOptionsAcceptableRangeMax.Visibility = spOptionsAcceptableRangeMin.Visibility = spOptionsLabel.Visibility = spOptionsValue.Visibility = Visibility.Hidden;
                xmlData2.Clear();
            }
            else
            {
                LoadOptionsForCertainIndex(0);
            }
        }
        UpdateTestEstimate();
    }

然后,我在名为 ORCTests 的同一解决方案中设置了一个单独的项目。在 ORCTests 中一个名为 UnitTest1.cs 的类中,我有以下测试...

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WpfTester;

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Reflection;
using System.Windows.Media.Imaging;
using System.Xml;


namespace ORCTests          
{
    [TestClass]
    public class UnitTest1 
    {
        [TestMethod]
        public void TestMethod1()
        {
            var wpf = new MainWindow();            
            wpf.LoadDeviceBox();
            Assert.IsTrue(WpfTester.MainWindow.xmlData.Count > 0);
        }
    }
}

当我调试它时,我进入 MainWindow() 但在 MainWindow 的第一行出现错误,它只是设置了动态路径。这是 MainWindow() 的第一行...

string binaryDocumentPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "/Documents/";

在调试模式下,我在这一行收到以下错误......

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=WpfTester
  StackTrace:
   at WpfTester.MainWindow..ctor() in C:\Users\StarkS02\source\repos\WpfTester\WpfTester\MainWindow.xaml.cs:line 26
   at ORCTests.UnitTest1.TestMethod1() in C:\Users\StarkS02\source\repos\WpfTester\ORCTests\UnitTest1.cs:line 24

当我正常运行此方法时,而不是在单元测试中,我在这一行没有收到错误。任何想法为什么我会收到此错误?

【问题讨论】:

    标签: c# wpf unit-testing


    【解决方案1】:

    恐怕这是因为 Test 类库不是 Windows 应用程序。因此它不能有 GUI。顺便说一句,在单元测试中,您必须测试与外界隔离的逻辑,并且在我看来,实例化窗口实例对于单元测试来说不是正确的事情。 尝试从窗口类中提取你的方法并单独测试。

    【讨论】:

      【解决方案2】:

      您尝试做的通常是使用自动化测试用例(而不是单元测试)来完成。应该使用单元测试来测试不依赖于系统的代码。

      您可以移动您的逻辑以使用 MVVM 实现它,然后可以为 Viewmodel 逻辑添加单元测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多