测试返回 IEnumerable<string> 的函数 [英] Testing a function that returns IEnumerable&lt;string&gt;

查看:37
本文介绍了测试返回 IEnumerable<string> 的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指导我如何为返回 Ienumerable 的方法编写测试吗?

Could someone guide me how to write a test for a method that returns Ienumerable ?

这是我的方法-

public  IEnumerable<string> fetchFiles()
{ 
   IOWrapper ioWrapper = new IOWrapper();
   var files = ioWrapper.GetFiles(folderPath, "*.csv", SearchOption.AllDirectories);
   return files;
}

我刚刚开始学习单元测试.因此,如果有人向我解释如何去做,我将不胜感激.

I've just started learning about unit-tests. So I would really appreciate if someone explains me how to go about it.

推荐答案

您需要一些测试框架.您可以使用嵌入在 Visual Studio 中的 MS 测试 - 向您的解决方案添加一个新的测试项目.或者你可以使用,例如,我正在使用的 NUnit.

You need some testing framework. You can use MS Test embedded in Visual Studio - add a new Test project to your solution. Or you can use, for instance, NUnit which I am using.

[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 指出 CollectionAssert班级.

For a complete list of possible asserts in NUnit navigate here. Also thanks to user xxMUROxx for pointing out the CollectionAssert class.

此外,对于多行的测试方法,您可能希望它具有可读性,因此请在互联网上搜索Arrange Act Assert (AAA) Pattern".

Additionaly, for test methods with many lines you would probably want it to be readable, so search for "Arrange Act Assert (AAA) Pattern" on the internet.

还有一点,关于在 SO 上此处测试 IEnumerables 已经有一个问题.

One more thing, there is already one question about testing IEnumerables here on SO.

这篇关于测试返回 IEnumerable<string> 的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆