Note that there are some explanatory texts on larger screens.

plurals
  1. POnunit setup/teardown not working?
    text
    copied!<p>Ok, I've got a strange problem. I am testing a usercontrol and have code like this:</p> <pre><code>[TestFixture] public myTestClass : UserControl { MyControl m_Control; [Test] public void TestMyControl() { m_Control = new MyControl(); this.Controls.Add(m_Control); Assert.That(/*SomethingOrOther*/) } } </code></pre> <p>This works fine, but when I change it to:</p> <pre><code>[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*/); } } </code></pre> <p>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.</p> <p>edit> The exact code is:</p> <pre><code>[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"); } } </code></pre> <p>I get the exception(s) at the respective m_MoneyBox.Focus() calls.</p> <p>Solved - See Joseph's comments</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload