Ok, I've got a strange problem. I am testing a usercontrol and have code like this:
[TestFixture]
public myTestClass : UserControl
{
MyControl m_Control;
[Test]
public void TestMyControl()
{
m_Control = new MyControl();
this.Controls.Add(m_Control);
Assert.That(/*SomethingOrOther*/)
}
}
This works fine, but when I change it to:
[TestFixture]
public myTestClass : UserControl
{
MyControl m_Control;
[Setup]
public void Setup()
{
m_Control = new MyControl();
this.Controls.Add(m_Control);
}
[TearDown]
public void TearDown()
{
this.Controls.Clear();
}
[Test]
public void TestMyControl()
{
Assert.That(/*SomethingOrOther*/);
}
}
I get an Object Reference Not Set To An Instance of an Object. I even output to the console to ensure that the setup/teardown were running at the correct times, and they were... but still it isn't newing up the usercontrols.
edit> The exact code is:
[TestFixture]
public class MoneyBoxTests : UserControl
{
private MoneyBox m_MoneyBox;
private TextBox m_TextBox;
#region "Setup/TearDown"
[SetUp]
public void Setup()
{
MoneyBox m_MoneyBox = new MoneyBox();
TextBox m_TextBox = new TextBox();
this.Controls.Add(m_MoneyBox);
this.Controls.Add(m_TextBox);
}
[TearDown]
public void TearDown()
{
this.Controls.Clear();
}
#endregion
[Test]
public void AmountConvertsToDollarsOnLeave()
{
m_MoneyBox.Focus();
m_MoneyBox.Text = "100";
m_TextBox.Focus();
Assert.That(m_MoneyBox.Text, Is.EqualTo("$100.00"), "Text isn't $100.00");
}
[Test]
public void AmountStaysANumberAfterConvertToDollars()
{
m_MoneyBox.Focus();
m_MoneyBox.Text = "100";
m_TextBox.Focus();
Assert.That(m_MoneyBox.Amount, Is.EqualTo(100), "Amount isn't 100");
}
}
I get the exception(s) at the respective m_MoneyBox.Focus() calls.
Solved - See Joseph's comments