失去委托价值领域

发布于 2024-10-12 05:58:50 字数 1607 浏览 2 评论 0 原文

我试图了解这里发生的事情。我正在为异步远程处理代码编写一些单元测试

    // a global variable to all unit tests in the class
    private List<ModuleInfo> _moduleInfo;

    [TestMethod]
    public void MyFunction()
    {
           _moduleInfo = new List<ModuleInfo>();

            netCall.MessageRecieved +=
                    delegate(object sender, MessageTestRecievedEventArgs e)
                    {
                       // I get a correct response - array of Modules
                       // then try to add to global variable
                       foreach (EducateMe.Shared.Types.ModuleInfo mIn in arr)
                       {
                           _moduleInfo.Add(mIn);
                       }
                    }

    }

// 因此在循环之后变量 _moduleInfo count = 9 // 下一个运行的测试,但是变量是空的 - 所以当它离开闭包时,它会以某种方式重置 - 我如何在测试之间保留这个值?

更新 -

这就是数组的定义方式。没有使用 [Setup] 或 [TearDown]。

[TestClass]
public class MyUnitTest
{
  private List<ModuleInfo> _moduleInfo;

  // then the function definition

}

还有第二个单元测试,它只是尝试读取循环创建的值。所以 MyFunction() 测试通过得很好,但是一旦退出闭包,变量就消失了。

干杯

PS。这是 VS 创建的单元测试附带的

    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }

I'm trying to understand whats happening here. I'm writing some unit tests for asynch'd remoting code

    // a global variable to all unit tests in the class
    private List<ModuleInfo> _moduleInfo;

    [TestMethod]
    public void MyFunction()
    {
           _moduleInfo = new List<ModuleInfo>();

            netCall.MessageRecieved +=
                    delegate(object sender, MessageTestRecievedEventArgs e)
                    {
                       // I get a correct response - array of Modules
                       // then try to add to global variable
                       foreach (EducateMe.Shared.Types.ModuleInfo mIn in arr)
                       {
                           _moduleInfo.Add(mIn);
                       }
                    }

    }

// so after the loop the variable _moduleInfo count = 9
// next Test that runs however the variable is empty - so when it leaves the closure it gets reset somehow - how might I preserve this value between tests?

Update -

This is how the array is defined. There's no [Setup] or [TearDown] being used.

[TestClass]
public class MyUnitTest
{
  private List<ModuleInfo> _moduleInfo;

  // then the function definition

}

There is a second unit test that is simply an attempt to read the value created by the loop. So MyFunction() test passes fine, but once it exits the closure the variable is gone.

Cheers

PS. This has come with the VS-created unit test

    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
        get
        {
            return testContextInstance;
        }
        set
        {
            testContextInstance = value;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮华 2024-10-19 05:58:50

您确定这不仅仅是为每个测试创建测试类的新实例吗?对我来说,这似乎是相当合理的行为 - 一个测试不应该依赖于已执行的其他测试。看来您应该使用 [SetUp] 来代替。

Are you sure it's not just creating a new instance of your test class for each test? That seems pretty reasonable behaviour to me - one test shouldn't rely on other tests having executed. It looks like you should be using [SetUp] instead.

疑心病 2024-10-19 05:58:50

不知道您使用的是哪个测试框架,但在 NUnit 中有一些方法可以具有属性 [Setup][TearDown]。也许这些函数之一会重置列表?

尽管如此,这对于单元测试来说是一个非常糟糕的设计。通过这种方式,您可以设计两个测试之间的耦合。因此,如果不运行第一个测试,就永远无法运行第二个测试,如何确保测试按什么顺序运行?

因此,请编写一些实用函数,在每个测试中使用一些包含预期值的存根来填充 _moduleInfo

关于如何进行测试的一个非常好的来源是《单元测试的艺术:带有示例》一书在.Net中。

Don't know which Testing Framework you are using but in in NUnit there are methods, that can have the attribute [Setup] and [TearDown]. Maybe one of these functions resets the list?

Nevertheless this is a very bad design for unit tests. By this way you design a coupling between two test. So you can never run the second test without running the first test and how do you ensure in which order tests are run?

So instead write some utility function that fills up _moduleInfo within each test with some stubs that are containing the expected values.

A very good source about how to do testing is the book The Art of Unit Testing: With Examples in .Net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文