如何使用 NUnit 测试回调
当你用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 lambda 表达式,则可以避免对私有字段的需要,这就是我通常这样做的方式。
它使您的测试更具凝聚力,并且在任何测试类上拥有字段总是会带来灾难!
You could avoid the need for private fields if you use a lambda expression, that's how I usually do this.
it makes your tests more cohesive and having fields on any test class is always asking for disaster!
据我所知,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.
从您的示例中,我无法确切地告诉您要做什么,但是,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