@@ -29,6 +29,56 @@ public void MockFile_AppendAllText_ShouldPersistNewText()
2929 file . ReadAllText ( path ) ) ;
3030 }
3131
32+ [ Test ]
33+ public void MockFile_AppendAllText_ShouldCreateIfNotExist ( )
34+ {
35+ // Arrange
36+ string path = @"c:\something\demo.txt" ;
37+ var fileSystem = new MockFileSystem ( new Dictionary < string , MockFileData >
38+ {
39+ { path , new MockFileData ( "Demo text content" ) }
40+ } ) ;
41+
42+ // Act
43+ var path2 = @"c:\something\demo2.txt" ;
44+ fileSystem . File . AppendAllText ( path2 , "some text" ) ;
45+ var path3 = @"c:\something\demo3.txt" ;
46+ fileSystem . File . AppendAllText ( path3 , "some text" , Encoding . Unicode ) ;
47+
48+ // Assert
49+ Assert . AreEqual (
50+ "some text" ,
51+ fileSystem . File . ReadAllText ( path2 ) ) ;
52+ Assert . AreEqual (
53+ "some text" ,
54+ fileSystem . File . ReadAllText ( path3 , Encoding . Unicode ) ) ;
55+ }
56+
57+ [ Test ]
58+ public void MockFile_AppendAllText_ShouldFailIfNotExistButDirectoryAlsoNotExist ( )
59+ {
60+ // Arrange
61+ string path = @"c:\something\demo.txt" ;
62+ var fileSystem = new MockFileSystem ( new Dictionary < string , MockFileData >
63+ {
64+ { path , new MockFileData ( "Demo text content" ) }
65+ } ) ;
66+
67+ var file = new MockFile ( fileSystem ) ;
68+
69+
70+ // Act
71+ path = @"c:\something2\demo.txt" ;
72+
73+ // Assert
74+ Exception ex ;
75+ ex = Assert . Throws < DirectoryNotFoundException > ( ( ) => fileSystem . File . AppendAllText ( path , "some text" ) ) ;
76+ Assert . That ( ex . Message , Is . EqualTo ( String . Format ( "Could not find a part of the path '{0}'." , path ) ) ) ;
77+
78+ ex = Assert . Throws < DirectoryNotFoundException > ( ( ) => fileSystem . File . AppendAllText ( path , "some text" , Encoding . Unicode ) ) ;
79+ Assert . That ( ex . Message , Is . EqualTo ( String . Format ( "Could not find a part of the path '{0}'." , path ) ) ) ;
80+ }
81+
3282 [ Test ]
3383 public void MockFile_AppendAllText_ShouldPersistNewTextWithCustomEncoding ( )
3484 {
0 commit comments