【发布时间】: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