有人可以指导我如何为返回 Ienumerable 的方法编写测试吗?
这是我的方法 -
public IEnumerable<string> fetchFiles()
{
IOWrapper ioWrapper = new IOWrapper();
var files = ioWrapper.GetFiles(folderPath, "*.csv", SearchOption.AllDirectories);
return files;
}
我刚刚开始学习单元测试。因此,如果有人解释我如何去做,我将非常感激。
您需要一些测试框架。您可以使用 Visual Studio 中嵌入的 MS Test - 将新的测试项目添加到您的解决方案中。或者您可以使用我正在使用的 NUnit。
[TestFixture] // NUnit attribute for a test class
public class MyTests
{
[Test] // NUnit attribute for a test method
public void fetchFilesTest() // name of a method you are testing + Test is a convention
{
var files = fetchFiles();
Assert.NotNull(files); // should pass if there are any files in a directory
Assert. ... // assert any other thing you are sure about, like if there is a particular file, or specific number of files, and so forth
}
}
有关 NUnit 中可能断言的完整列表,请导航。也感谢用户 xxMUROxx 指出 class.
此外,对于多行测试方法,您可能希望它可读,因此在互联网上搜索“Arrange Act Assert (AAA) Pattern”。
另一件事,已经有一个关于测试 IEnumerables 的问题here https://stackoverflow.com/questions/545671/assert-ienumerables?rq=1 on SO.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)