Skip to content

Commit 980a57a

Browse files
committed
Clean up readme a bit
1 parent 6c33c36 commit 980a57a

File tree

1 file changed

+36
-50
lines changed

1 file changed

+36
-50
lines changed

README.md

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and/or:
1313

1414
Install-Package System.IO.Abstractions.TestingHelpers
1515

16-
At the core of the library is IFileSystem and FileSystem. Instead of calling methods like `File.ReadAllText` directly, use `IFileSystem.File.ReadAllText`. We have exactly the same API, except that ours is injectable and testable.
16+
At the core of the library is `IFileSystem` and `FileSystem`. Instead of calling methods like `File.ReadAllText` directly, use `IFileSystem.File.ReadAllText`. We have exactly the same API, except that ours is injectable and testable.
1717

1818
```csharp
1919
public class MyComponent
@@ -28,7 +28,7 @@ public class MyComponent
2828
/// <summary>Create MyComponent</summary>
2929
public MyComponent() : this(
3030
fileSystem: new FileSystem() //use default implementation which calls System.IO
31-
)
31+
)
3232
{
3333
}
3434

@@ -74,81 +74,67 @@ public void MyComponent_Validate_ShouldThrowNotSupportedExceptionIfTestingIsNotA
7474
Assert.Fail("The expected exception was not thrown.");
7575
}
7676
```
77+
7778
We even support casting from the .NET Framework's untestable types to our testable wrappers:
7879

7980
```csharp
80-
FileInfo SomeBadApiMethodThatReturnsFileInfo()
81+
FileInfo SomeApiMethodThatReturnsFileInfo()
8182
{
8283
return new FileInfo("a");
8384
}
8485

8586
void MyFancyMethod()
8687
{
87-
var testableFileInfo = (FileInfoBase)SomeBadApiMethodThatReturnsFileInfo();
88+
var testableFileInfo = (FileInfoBase)SomeApiMethodThatReturnsFileInfo();
8889
...
8990
}
9091
```
9192

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):
93+
Since version 4.0 the top-level APIs expose interfaces instead of abstract base classes (these still exist, though), allowing you to completely mock the file system. Here's a small example, using [Moq](https://github.com/moq/moq4):
9694

9795
```csharp
98-
using Moq;
99-
using NUnit.Framework;
100-
using System;
101-
using System.IO.Abstractions;
102-
103-
namespace Tests
96+
[Test]
97+
public void Test1()
10498
{
105-
public class Tests
106-
{
107-
[Test]
108-
public void Test1()
109-
{
110-
var watcher = Mock.Of<IFileSystemWatcher>();
111-
var file = Mock.Of<IFile>();
99+
var watcher = Mock.Of<IFileSystemWatcher>();
100+
var file = Mock.Of<IFile>();
112101

113-
Mock.Get(file).Setup(f => f.Exists(It.IsAny<string>())).Returns(true);
114-
Mock.Get(file).Setup(f => f.ReadAllText(It.IsAny<string>())).Throws<OutOfMemoryException>();
102+
Mock.Get(file).Setup(f => f.Exists(It.IsAny<string>())).Returns(true);
103+
Mock.Get(file).Setup(f => f.ReadAllText(It.IsAny<string>())).Throws<OutOfMemoryException>();
115104

116-
var unitUnderTest = new SomeClassUsingFileSystemWatcher(watcher, file);
105+
var unitUnderTest = new SomeClassUsingFileSystemWatcher(watcher, file);
117106

118-
Assert.Throws<OutOfMemoryException>(() => {
119-
Mock.Get(watcher).Raise(w => w.Created += null, new System.IO.FileSystemEventArgs(System.IO.WatcherChangeTypes.Created, @"C:\Some\Directory", "Some.File"));
120-
});
107+
Assert.Throws<OutOfMemoryException>(() => {
108+
Mock.Get(watcher).Raise(w => w.Created += null, new System.IO.FileSystemEventArgs(System.IO.WatcherChangeTypes.Created, @"C:\Some\Directory", "Some.File"));
109+
});
121110

122-
Mock.Get(file).Verify(f => f.Exists(It.IsAny<string>()), Times.Once);
111+
Mock.Get(file).Verify(f => f.Exists(It.IsAny<string>()), Times.Once);
123112

124-
Assert.True(unitUnderTest.FileWasCreated);
125-
}
126-
}
113+
Assert.True(unitUnderTest.FileWasCreated);
114+
}
127115

128-
public class SomeClassUsingFileSystemWatcher
129-
{
130-
private readonly IFileSystemWatcher _watcher;
131-
private readonly IFile _file;
116+
public class SomeClassUsingFileSystemWatcher
117+
{
118+
private readonly IFileSystemWatcher _watcher;
119+
private readonly IFile _file;
132120

133-
public bool FileWasCreated { get; private set; }
121+
public bool FileWasCreated { get; private set; }
134122

135-
public SomeClassUsingFileSystemWatcher(IFileSystemWatcher watcher, IFile file)
136-
{
137-
this._file = file;
138-
this._watcher = watcher;
139-
this._watcher.Created += Watcher_Created;
140-
}
123+
public SomeClassUsingFileSystemWatcher(IFileSystemWatcher watcher, IFile file)
124+
{
125+
this._file = file;
126+
this._watcher = watcher;
127+
this._watcher.Created += Watcher_Created;
128+
}
141129

142-
private void Watcher_Created(object sender, System.IO.FileSystemEventArgs e)
143-
{
144-
FileWasCreated = true;
130+
private void Watcher_Created(object sender, System.IO.FileSystemEventArgs e)
131+
{
132+
FileWasCreated = true;
145133

146-
if(_file.Exists(e.FullPath))
147-
{
148-
var text = _file.ReadAllText(e.FullPath);
149-
}
134+
if(_file.Exists(e.FullPath))
135+
{
136+
var text = _file.ReadAllText(e.FullPath);
150137
}
151138
}
152139
}
153-
154140
```

0 commit comments

Comments
 (0)