Skip to content

Commit 223504f

Browse files
authored
refactor: cleanup temporary file created in unit test (#600)
In the `Method_Wrap_FileStream_ShouldRegisterCall` test, in order to get a valid `FileStream` a temporary test file has to be created on the real file system. This file should be cleaned up after the test execution is finished.
1 parent 64be166 commit 223504f

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileStreamFactoryStatisticsTests.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,29 @@ public void Method_New_String_FileStreamOptions_ShouldRegisterCall()
189189
[SkippableFact]
190190
public void Method_Wrap_FileStream_ShouldRegisterCall()
191191
{
192+
string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
192193
MockFileSystem sut = new();
193-
FileStream fileStream = new("foo", FileMode.OpenOrCreate);
194-
195194
try
196195
{
197-
using FileSystemStream result = sut.FileStream.Wrap(fileStream);
196+
using FileStream fileStream = new(path, FileMode.OpenOrCreate);
197+
198+
try
199+
{
200+
using FileSystemStream result = sut.FileStream.Wrap(fileStream);
201+
}
202+
catch (NotSupportedException)
203+
{
204+
// Wrap is not possible on the MockFileSystem, but should still be registered!
205+
}
206+
207+
sut.StatisticsRegistration.TotalCount.Should().Be(1);
208+
sut.Statistics.FileStream.ShouldOnlyContainMethodCall(nameof(IFileStreamFactory.Wrap),
209+
fileStream);
198210
}
199-
catch (NotSupportedException)
211+
finally
200212
{
201-
// Wrap is not possible on the MockFileSystem, but should still be registered!
213+
File.Delete(path);
202214
}
203-
204-
sut.StatisticsRegistration.TotalCount.Should().Be(1);
205-
sut.Statistics.FileStream.ShouldOnlyContainMethodCall(nameof(IFileStreamFactory.Wrap),
206-
fileStream);
207215
}
208216

209217
[SkippableFact]

0 commit comments

Comments
 (0)