Skip to content

Commit a31f0f2

Browse files
WeiermannWeiermann
authored andcommitted
Added small note about 4.0 and a little example
1 parent 3e31d24 commit a31f0f2

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,50 @@ void MyFancyMethod()
8888
...
8989
}
9090
```
91+
92+
### New in 4.0:
93+
In version 4.0, the api introduces yet another layer of abstraction; instead of using abstract base classes (these still exist, though), things were changed to use interfaces instead.
94+
95+
Using these allows you to completely mock the file system using your mocking library of choice. Here's a small example (using Moq):
96+
97+
```csharp
98+
using Moq;
99+
using NUnit.Framework;
100+
using System.IO.Abstractions;
101+
102+
namespace Tests
103+
{
104+
public class Tests
105+
{
106+
[Test]
107+
public void Test1()
108+
{
109+
var watcher = Mock.Of<IFileSystemWatcher>();
110+
111+
var unitUnderTest = new SomeClassUsingFileSystemWatcher(watcher);
112+
113+
Mock.Get(watcher).Raise(w => w.Created += null, new System.IO.FileSystemEventArgs(System.IO.WatcherChangeTypes.Created, @"C:\Some\Directory", "Some.File"));
114+
115+
Assert.True(unitUnderTest.FileWasCreated);
116+
}
117+
}
118+
119+
public class SomeClassUsingFileSystemWatcher
120+
{
121+
private readonly IFileSystemWatcher watcher;
122+
123+
public bool FileWasCreated { get; private set; }
124+
125+
public SomeClassUsingFileSystemWatcher(IFileSystemWatcher watcher)
126+
{
127+
this.watcher = watcher;
128+
this.watcher.Created += Watcher_Created;
129+
}
130+
131+
private void Watcher_Created(object sender, System.IO.FileSystemEventArgs e)
132+
{
133+
FileWasCreated = true;
134+
}
135+
}
136+
}
137+
```

0 commit comments

Comments
 (0)