简体   繁体   English

nunit 设置/拆卸不起作用?

[英]nunit setup/teardown not working?

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.我得到一个 Object 参考未设置为 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.我什至将 output 连接到控制台,以确保设置/拆卸在正确的时间运行,它们是……但它仍然没有更新用户控件。

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.我在各自的 m_MoneyBox.Focus() 调用中得到异常。

Solved - See Joseph's comments解决了 - 见约瑟夫的评论

You haven't said where you're getting the exception, which would help - what does the stack trace look like?你还没有说你从哪里得到异常,这会有所帮助 - 堆栈跟踪是什么样的?

It's very odd (IME) to derive from UserControl when you create a test fixture.创建测试夹具时从 UserControl 派生是非常奇怪的 (IME)。 Aside from anything else, I don't know that NUnit is going to call Dispose for you at any appropriate point... what's the purpose of it here?除此之外,我不知道 NUnit 会在任何适当的时候为您调用 Dispose ......这里的目的是什么? Can you not make your tests run with a "plain" test fixture?你不能让你的测试用一个“普通”的测试夹具运行吗?

I created a test case with exactly the same layout you presented here, but with a TextBox instead of a MyControl.我创建了一个与您在此处展示的布局完全相同的测试用例,但使用的是 TextBox 而不是 MyControl。 I also added a constructor and a deconstructor and outputted all the various stages to the console to see the sequence of events.我还添加了一个构造函数和一个解构函数,并将所有不同的阶段输出到控制台以查看事件的顺序。 However, I never got an object reference exception.但是,我从来没有遇到过 object 引用异常。

In case you are interested, the sequence was [constructor called], [setup called], [test called], [tear down called].如果您有兴趣,顺序是[构造函数调用]、[设置调用]、[测试调用]、[拆卸调用]。 The deconstruction never output anything to the screen for some reason.由于某种原因,解构从未出现在屏幕上。

My original thought was that the Controls property on myTestClass would not be initialized, but on my test it was, so I think it has something to do with your MyControl construction.我最初的想法是 myTestClass 上的 Controls 属性不会被初始化,但在我的测试中它是,所以我认为它与您的 MyControl 构造有关。

edit> I added the focus on my TextBox in my unit test as well but still no exception.编辑> 我在单元测试中也将焦点放在了我的 TextBox 上,但仍然没有例外。 Does your MoneyBox have any event handling going on behind the scenes during the Focus?在 Focus 期间,您的 MoneyBox 是否在幕后进行任何事件处理? That might be your culprit.那可能是你的罪魁祸首。

I had exactly the same issue, so my apologies for answer to this old post.我有完全相同的问题,所以我很抱歉回答这个旧帖子。 The problem in your code (and mine) is that you are creating 2 different instances for MoneyBox and 2 more for TextBox.您的代码(和我的)中的问题是您正在为 MoneyBox 创建 2 个不同的实例,为 TextBox 创建另外 2 个实例。 So, the initial assignation inside Setup, is valid only for Setup method and out_of_scope in the test methods.因此,Setup 内部的初始分配仅对测试方法中的 Setup 方法和 out_of_scope 有效。

Inside the Setup method you should use:在 Setup 方法中,您应该使用:

m_MoneyBox = new MoneyBox(); //GOOD
m_TextBox = new TextBox();  //GOOD

instead of代替

MoneyBox m_MoneyBox = new MoneyBox();  //BAD
TextBox m_TextBox = new TextBox();  //BAD

Just for anyone that could need it again只为任何可能再次需要它的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM