Testing a function that returns IEnumerable

前端 未结 2 1783
余生分开走
余生分开走 2021-01-27 05:34

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

Here\'s my method -

public  IEnumerable fetchFiles()
{          


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-27 05:56

    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
        }
    }
    

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

    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.

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

提交回复
热议问题