Reputation: 79
I'm attempting to set up unit tests for a WPF project I have. I have a public method called MoveSelectionOutOfSelectedBox in a project called WPFTester. Here is the method...
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();
}
I then set up a separate project in the same solution called ORCTests. Within a class in ORCTests called UnitTest1.cs, I have the following test...
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);
}
}
}
When I debug this I step into MainWindow() but get an error on the first line of MainWindow which simply sets up dynamic paths. Here is the first line of MainWindow()...
string binaryDocumentPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "/Documents/";
In debug mode I get this following error on this line....
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
When I run this method normally, not in a unit test, I do not get an error on this line. Any ideas why I get this error?
Upvotes: 1
Views: 487
Reputation: 19
What you are trying to do is generally done using Automated test cases (not unit tests). Unit tests should be used to test the code which is not dependent of System.
You can move your logic to implement it using MVVM and then can add Unit test for Viewmodel logic.
Upvotes: 0
Reputation: 929
I am afraid this is because Test class library isn't a windows application. Therefore it cannot have a GUI. By the way, in unit tests, you have to test your logic isolated from the outside world, and instantiating window instances isn't a correct thing for the unit tests in my opinion. Try to extract your method from the window class and test it separately.
Upvotes: 1