@@ -42,6 +42,7 @@ public void MockFile_Open_CreatesNewFileFileOnCreate()
4242 {
4343 string filepath = XFS . Path ( @"c:\something\doesnt\exist.txt" ) ;
4444 var filesystem = new MockFileSystem ( new Dictionary < string , MockFileData > ( ) ) ;
45+ filesystem . AddDirectory ( @"c:\something\doesnt" ) ;
4546
4647 var stream = filesystem . File . Open ( filepath , FileMode . Create ) ;
4748
@@ -55,6 +56,7 @@ public void MockFile_Open_CreatesNewFileFileOnCreateNew()
5556 {
5657 string filepath = XFS . Path ( @"c:\something\doesnt\exist.txt" ) ;
5758 var filesystem = new MockFileSystem ( new Dictionary < string , MockFileData > ( ) ) ;
59+ filesystem . AddDirectory ( @"c:\something\doesnt" ) ;
5860
5961 var stream = filesystem . File . Open ( filepath , FileMode . CreateNew ) ;
6062
@@ -153,6 +155,7 @@ public void MockFile_Open_CreatesNewFileOnOpenOrCreate()
153155 {
154156 string filepath = XFS . Path ( @"c:\something\doesnt\exist.txt" ) ;
155157 var filesystem = new MockFileSystem ( new Dictionary < string , MockFileData > ( ) ) ;
158+ filesystem . AddDirectory ( @"c:\something\doesnt" ) ;
156159
157160 var stream = filesystem . File . Open ( filepath , FileMode . OpenOrCreate ) ;
158161
@@ -240,5 +243,20 @@ public void MockFile_OpenText_ShouldRetainCreationTime()
240243 // Assert
241244 Assert . AreEqual ( creationTime , fs . FileInfo . FromFileName ( filepath ) . CreationTime ) ;
242245 }
246+
247+ [ Test ]
248+ public void MockFile_Open_ShouldThrowDirectoryNotFoundExceptionIfFileModeCreateAndParentPathDoesNotExist ( )
249+ {
250+ // Arrange
251+ var fileSystem = new MockFileSystem ( ) ;
252+
253+ // Act
254+ TestDelegate action = ( ) => fileSystem . File . Open ( "C:\\ Path\\ NotFound.ext" , FileMode . Create ) ;
255+
256+ // Assert
257+ Assert . IsFalse ( fileSystem . Directory . Exists ( "C:\\ path" ) ) ;
258+ var exception = Assert . Throws < DirectoryNotFoundException > ( action ) ;
259+ Assert . That ( exception . Message , Does . StartWith ( "Could not find a part of the path" ) ) ;
260+ }
243261 }
244262}
0 commit comments