如何使用 NUnit 测试回调

发布于 2024-09-15 12:05:00 字数 774 浏览 4 评论 0原文

当你用NUnit测试回调时,有什么特殊的支持吗?或者某种比我下面的解决方案更好的“最佳实践”?

我刚刚开始编写一些测试和方法,所以我仍然可以完全控制 - 但是我认为如果有更好的方法来彻底测试回调,尤其是在复杂性增加的情况下,这可能会很烦人。这是我现在正在测试的一个简单示例:

要测试的方法使用委托,它会调用回调函数,例如在流中发现新的 xml 元素时。出于测试目的,我将 NewElementCallback 方法传递给委托,并在调用函数时将参数内容存储在一些测试类属性中。我将这些属性用于断言。 (当然它们在测试设置中被重置)

[Test]
public void NewElement()
{
    String xmlString = @"<elem></elem>";

    this.xml.InputStream = new StringReader(xmlString);
    this.xml.NewElement += this.NewElementCallback;

    this.xml.Start();

    Assert.AreEqual("elem", this.elementName);
    Assert.AreEqual(0, this.elementDepth);
}

private void NewElementCallback(string elementName, int elementDepth)
{
    this.elementName = elementName;
    this.elementDepth = elementDepth;
}

Is there any special support when you come to test callbacks with NUnit? Or some kind of of "best practice" which is better than my solution below?

I just started writing some tests and methods, so I have still full control - however I think it might be annoying if there are better ways to test callbacks thoroughly, especially if complexity is increasing. So this is a simple example how I am testing right now:

The method to be tested uses a delegate, which calls a Callback function, for instance as soon as a new xml element is being discovered in a stream. For testing purpose I pass the NewElementCallback Method to the delegate and store the arguments content in some test classes properties when the function is called. These properties I use for assertion. (Of course they are being reset in the test setup)

[Test]
public void NewElement()
{
    String xmlString = @"<elem></elem>";

    this.xml.InputStream = new StringReader(xmlString);
    this.xml.NewElement += this.NewElementCallback;

    this.xml.Start();

    Assert.AreEqual("elem", this.elementName);
    Assert.AreEqual(0, this.elementDepth);
}

private void NewElementCallback(string elementName, int elementDepth)
{
    this.elementName = elementName;
    this.elementDepth = elementDepth;
}

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

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

发布评论

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

评论(3

屌丝范 2024-09-22 12:05:00

如果您使用 lambda 表达式,则可以避免对私有字段的需要,这就是我通常这样做的方式。

[Test]
public void NewElement()
{
    String xmlString = @"<elem></elem>";
    string elementName;
    int elementDepth;

    this.xml.InputStream = new StringReader(xmlString);
    this.xml.NewElement += (name,depth) => { elementName = name; elementDepth = depth };

    this.xml.Start();

    Assert.AreEqual("elem", elementName);
    Assert.AreEqual(0, elementDepth);
}

它使您的测试更具凝聚力,并且在任何测试类上拥有字段总是会带来灾难!

You could avoid the need for private fields if you use a lambda expression, that's how I usually do this.

[Test]
public void NewElement()
{
    String xmlString = @"<elem></elem>";
    string elementName;
    int elementDepth;

    this.xml.InputStream = new StringReader(xmlString);
    this.xml.NewElement += (name,depth) => { elementName = name; elementDepth = depth };

    this.xml.Start();

    Assert.AreEqual("elem", elementName);
    Assert.AreEqual(0, elementDepth);
}

it makes your tests more cohesive and having fields on any test class is always asking for disaster!

青萝楚歌 2024-09-22 12:05:00

据我所知,NUnit 对此没有什么特别的。我和你一样测试这些东西。我确实倾向于将回调方法及其存储的状态放在另一个类上。我认为这使它更干净一些,但并没有本质上的不同。

There isn't anything special in NUnit for this that I know of. I test these things the same way you do. I do tend to put the callback method and the state it stores on another class. I think it makes it a bit cleaner, but it isn't fundamentally different.

眼藏柔 2024-09-22 12:05:00

从您的示例中,我无法确切地告诉您要做什么,但是,NUnit 没有提供任何特定的方法来测试此类事情,但是此链接应该为您提供一些有关如何开始单元测试的想法异步代码:单元测试异步代码

From your example, I can't tell exactly what you're trying to do, however, NUnit doesn't provide any specific way to test this kind of thing, however this link should present you some ideas on how to start unit-testing asynchronous code: Unit Testing Asynchronous code

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