@@ -62,6 +62,7 @@ public AndConstraint<DirectoryAssertions> HasDirectories(
6262 public AndWhichConstraint < FileSystemAssertions , DirectoryAssertions > HasDirectory (
6363 string searchPattern = "*" , string because = "" , params object [ ] becauseArgs )
6464 {
65+ var subdirectory = Subject ? . GetDirectories ( searchPattern ) . FirstOrDefault ( ) ;
6566 Execute . Assertion
6667 . WithDefaultIdentifier ( Identifier )
6768 . BecauseOf ( because , becauseArgs )
@@ -75,16 +76,22 @@ public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasDirector
7576 . Then
7677 . Given ( ( ) => Subject ! )
7778 . ForCondition ( directoryInfo
78- => directoryInfo . GetDirectories ( searchPattern ) . Length = = 1 )
79+ => directoryInfo . GetDirectories ( searchPattern ) . Length < = 1 )
7980 . FailWith (
8081 "Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found {2}." ,
8182 _ => searchPattern ,
8283 directoryInfo => directoryInfo . Name ,
83- directoryInfo => directoryInfo . GetDirectories ( searchPattern ) . Length ) ;
84+ directoryInfo => directoryInfo . GetDirectories ( searchPattern ) . Length )
85+ . Then
86+ . ForCondition ( _ => subdirectory != null )
87+ . FailWith (
88+ "Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found none." ,
89+ _ => searchPattern ,
90+ directoryInfo => directoryInfo . Name ) ;
8491
8592 return new AndWhichConstraint < FileSystemAssertions , DirectoryAssertions > (
8693 new FileSystemAssertions ( Subject ! . FileSystem ) ,
87- new DirectoryAssertions ( Subject ! . GetDirectories ( searchPattern ) . Single ( ) ) ) ;
94+ new DirectoryAssertions ( subdirectory ) ) ;
8895 }
8996
9097 /// <summary>
@@ -93,6 +100,7 @@ public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasDirector
93100 public AndWhichConstraint < FileSystemAssertions , FileAssertions > HasFile (
94101 string searchPattern = "*" , string because = "" , params object [ ] becauseArgs )
95102 {
103+ var file = Subject ? . GetFiles ( searchPattern ) . FirstOrDefault ( ) ;
96104 Execute . Assertion
97105 . WithDefaultIdentifier ( Identifier )
98106 . BecauseOf ( because , becauseArgs )
@@ -106,16 +114,22 @@ public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasFile(
106114 . Then
107115 . Given ( ( ) => Subject ! )
108116 . ForCondition ( directoryInfo
109- => directoryInfo . GetFiles ( searchPattern ) . Length = = 1 )
117+ => directoryInfo . GetFiles ( searchPattern ) . Length < = 1 )
110118 . FailWith (
111119 "Expected {context} {1} to contain exactly one file matching {0}{reason}, but found {2}." ,
112120 _ => searchPattern ,
113121 directoryInfo => directoryInfo . Name ,
114- directoryInfo => directoryInfo . GetFiles ( searchPattern ) . Length ) ;
122+ directoryInfo => directoryInfo . GetFiles ( searchPattern ) . Length )
123+ . Then
124+ . ForCondition ( _ => file != null )
125+ . FailWith (
126+ "Expected {context} {1} to contain exactly one file matching {0}{reason}, but found none." ,
127+ _ => searchPattern ,
128+ directoryInfo => directoryInfo . Name ) ;
115129
116130 return new AndWhichConstraint < FileSystemAssertions , FileAssertions > (
117131 new FileSystemAssertions ( Subject ! . FileSystem ) ,
118- new FileAssertions ( Subject ! . GetFiles ( searchPattern ) . Single ( ) ) ) ;
132+ new FileAssertions ( file ) ) ;
119133 }
120134
121135 /// <summary>
0 commit comments