Could someone guide me how to write a test for a method that returns Ienumerable ?
Here\'s my method -
public IEnumerable fetchFiles()
{
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.