Jetnor
Jetnor

Reputation: 531

Unit test success of multiple file delete method

I recently asked a question but but did not get an answer that I could act on. I think this was due to the long code sample included. I have decided to post another question with a much smaller code sample. I want to unit test the below method to make sure that it does work and to make sure that it deletes all .xml files in a specified directory.

private static void DeleteXmlFiles(string XmlFileDirectory)
{
    foreach (var file in Directory.GetFiles(XmlFileDirectory, "*.Xml"))
    {
        File.Delete(file);
    }
}

Does anybody have any unit testing code snippet that I can look at which would help me in this case?

The below is all i have in the Test method which basically is not much:

[Test]
public void can_delete_all_files_from_specified_directory()
{
    string inputDir = @"C:\TestFiles\";
    var sut = new FilesUtility();
    var deleteSuccess = sut.
}

Upvotes: 2

Views: 2713

Answers (3)

Charles Bretana
Charles Bretana

Reputation: 146409

one approach to such a test might be:

  1. Create a directory(folder) (in the unit test assembly folder)
  2. Stick some xml files into it (you might copy thwem from another folder in the unit test assembly folder)
  3. call you method that should delete them.
  4. Check to see if they are gone. a. If they are, report success, b. If not report failure
  5. Delete the folder created in step 1

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

In order to unit-test your method, you should test it in isolation. I.e. there should not be any real classes like Directory or File your SUT interacts with. So, you have three options:

  • Create abstraction which your class will depend on, and mock that abstraction for tests (Moq will do that)
  • Mock static methods for test. Moq can't help here, but that is possible with TypeMock, Moles, JustMock etc
  • Do acceptance or integration testing instead of unit testing (Specflow is good for writing acceptance tests)

Last approach is pretty simple - create new folder before each test runs, and delete it after test run

private string path =  @"C:\TestFiles\";

[SetUp]
public void Setup()
{
    Directory.CreateDirectory(path);        
}

[TearDown]
public void Deardown()
{
     Directory.Delete(path);
}

[Test]
public void ShouldRemoveAllFilesFromSpecifiedDirectory()
{
    // seed directory with sample files
    FileUtility.DeleteXmlFiles(path);
    Assert.False(Directory.EnumerateFiles(path).Any());
}

Upvotes: 3

Thomas Weller
Thomas Weller

Reputation: 11717

There's no way of testing methods like these with Moq or any other free mocking framework. That's because they cannot mock methods other than virtuals or interface implementations (which is roughly the same under the hood anyway).

To fake (not mock) system methods like File.Delete(...) or static methods of any kind, you'll need something like Typemock (commercial) or MS Moles (not very user-friendly).

As a workaround, you could create a test directory along with some files in your test, call DeleteXmlFiles(...) on it, and then check if the directory is empty. But that would be slow and also is not really a unit test but more like an integration test.

Upvotes: 1

Related Questions