0

I have a class that watches files using the following method:

public FileWatcher(List<string> dirsToWatch, string filter, OnChange onChange)
    {
        OnChangeEnt = onChange;

        foreach (var dir in dirsToWatch)
        {
            var watcher = new FileSystemWatcher(dir);
            watcher.Filter = filter;
            watcher.Changed += new FileSystemEventHandler(onChange);
            watcher.Created += new FileSystemEventHandler(onChange);
            watcher.Renamed += new RenamedEventHandler(OnChangeHandler);
            watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName;
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;
            _watchers.Add(watcher);
        }
    }

I just wanted to know how I would unit test this method to get 100% coverage. Any help would be truly appreciated.Thanks

Cory
  • 67
  • 1
  • 2
  • 11
  • I was thinking about unit testing similar functions, however as You can't simulate file depository, I would say You can't 100% cover such thing (opinion). – Tatranskymedved Feb 20 '17 at 17:36
  • 1
    The only thing I can think to test is that _watcher has the file system watchers added to for the dirsToWatch – Ken Tucker Feb 20 '17 at 17:39
  • http://stackoverflow.com/questions/1528134/unit-testing-file-i-o – Tatranskymedved Feb 20 '17 at 17:42
  • Are you able to modify that code under test? I would suggest abstracting the file watchers. – Nkosi Feb 20 '17 at 17:42
  • 1
    What exactly do you want to test, the configuration of the FileWatcher or the working? – Peter Bons Feb 20 '17 at 17:52
  • well if you want to test FileSystemWatcher class you need that source code. But you should not test other peoples (company's) code. – Jocke Feb 21 '17 at 07:50
  • hi @PeterBons, i want to test the working of the FileWatcher but i don't know how i should go about testing it or mocking it up. just wanted an example of a good unit test for it that covers most of it if its not possible to get 100% – Cory Feb 21 '17 at 09:24
  • Well, in my opinion you should test only the configuration and not the FileWatcher itself as it is part of the .Net Framework, like @Jocke states as well. I would only test whether the watcher is configured according to the method input. – Peter Bons Feb 21 '17 at 09:47
  • @PeterBons How would i test the configuration of the method above? – Cory Feb 21 '17 at 11:09

2 Answers2

3

You can always use mocking (like Typemock Isolator) which will isolate called methods from the system calls so you can mock their behavior and write tests that target only one separate use case in your method. You can also use some special directory that's just for this test, but you will have to write proper initialization and teardown scripts.

Martin Macak
  • 3,507
  • 2
  • 30
  • 54
  • 1
    Hi Martin, I managed to get 100% coverage using the Typemock Isolator method thanks – Cory Feb 21 '17 at 12:01
0

FileWatcher depends on the environment (file system in this case). Things which involve environment cannot be unit-tested. You can either:

  • Abstract functionality of FileSystemWatcher via some interface, make FileWatcher depend on that interface, and mock interface for unit-testing of FileWatcher. That makes sense when you have some relatively complex behavior which uses environment-dependant class. And you want to verify that behavior.

  • Do acceptance testing for your application. That will involve end-to-end tests with adding/removing/editing files in real file system and verifying that your application reacts to those changes as required.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459