@@ -97,6 +97,7 @@ Using these allows you to completely mock the file system using your mocking lib
9797``` csharp
9898using Moq ;
9999using NUnit .Framework ;
100+ using System ;
100101using System .IO .Abstractions ;
101102
102103namespace Tests
@@ -107,31 +108,47 @@ namespace Tests
107108 public void Test1 ()
108109 {
109110 var watcher = Mock .Of <IFileSystemWatcher >();
111+ var file = Mock .Of <IFile >();
110112
111- var unitUnderTest = new SomeClassUsingFileSystemWatcher (watcher );
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 >();
112115
113- Mock .Get (watcher ).Raise (w => w .Created += null , new System .IO .FileSystemEventArgs (System .IO .WatcherChangeTypes .Created , @" C:\Some\Directory" , " Some.File" ));
116+ var unitUnderTest = new SomeClassUsingFileSystemWatcher (watcher , file );
117+
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+ });
121+
122+ Mock .Get (file ).Verify (f => f .Exists (It .IsAny <string >()), Times .Once );
114123
115124 Assert .True (unitUnderTest .FileWasCreated );
116125 }
117126 }
118127
119128 public class SomeClassUsingFileSystemWatcher
120129 {
121- private readonly IFileSystemWatcher watcher ;
130+ private readonly IFileSystemWatcher _watcher ;
131+ private readonly IFile _file ;
122132
123133 public bool FileWasCreated { get ; private set ; }
124134
125- public SomeClassUsingFileSystemWatcher (IFileSystemWatcher watcher )
135+ public SomeClassUsingFileSystemWatcher (IFileSystemWatcher watcher , IFile file )
126136 {
127- this .watcher = watcher ;
128- this .watcher .Created += Watcher_Created ;
137+ this ._file = file ;
138+ this ._watcher = watcher ;
139+ this ._watcher .Created += Watcher_Created ;
129140 }
130141
131142 private void Watcher_Created (object sender , System.IO.FileSystemEventArgs e )
132143 {
133144 FileWasCreated = true ;
145+
146+ if (_file .Exists (e .FullPath ))
147+ {
148+ var text = _file .ReadAllText (e .FullPath );
149+ }
134150 }
135151 }
136152}
153+
137154```
0 commit comments