You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to write a test that checks for insufficient space available? #826
Describe the bug
I need to write a test for some logic that checks if there is enough space available on disk.
To Reproduce
logic to test
privateIFileSystem_fileSystem;//injected in ctor as = new MockFileSystem() in tests | = new FileSystem() in productionvarrequiredSpace=calculateActualSize(filePaths);varavailableFreeSpace=getAvailableFreeSpaceOnDisk(driveName);if(availableFreeSpace<requiredSpace){//throw exception}privatelonggetAvailableFreeSpaceOnDisk(stringdriveName){vardriveInfo=_fileSystem.DriveInfo.FromDriveName(driveName);varresult=driveInfo.AvailableFreeSpace;returnresult;}
This is currently not possible. As mentioned in the issue you linked these properties should actually be read-only.
Would be cool to support this somehow, but I guess it would be quite some effort. I will probably not find the time for implementing this anytime soon, but would be open for a contribution.
Thank you for your time and your reply.
In the meantime I managed to come up with a workaround.
I made my own IFileSystem by inheriting from MockFileSystem and used Moq. Maybe it is useful for someone else.
publicclassTestClass{[Fact]publicvoidCreateBackup_RequiredSpaceIsInsufficient_ShouldReturnFalse(){// ArrangevarsourceFilePath=$@"A:\source\test.txt";varfileSize=2;//setup DriveInfo for backupvarmockedDriveInfo=newMock<IDriveInfo>();mockedDriveInfo.Setup(x =>x.Name).Returns(()=>"Q");mockedDriveInfo.Setup(x =>x.AvailableFreeSpace).Returns(()=>fileSize-1);varmockedDriveInfoFactory=newMock<IDriveInfoFactory>();mockedDriveInfoFactory.Setup(x =>x.FromDriveName(It.IsAny<string>())).Returns(()=>mockedDriveInfo.Object);//add files to backup to FileSystemvarfileSystem=newMyMockFileSystem(mockedDriveInfoFactory.Object);fileSystem.AddFile(sourceFilePath,newMockFileData(newbyte[fileSize]));// ...}}internalclassMyMockFileSystem:MockFileSystem{privateIDriveInfoFactory_driveInfoFactory;publicMyMockFileSystem(IDriveInfoFactorydriveInfoFactoryToUse):base(){_driveInfoFactory=driveInfoFactoryToUse;}publicoverrideIDriveInfoFactoryDriveInfo=>_driveInfoFactory;}
In the meantime I managed to come up with a workaround. I made my own IFileSystem by inheriting from MockFileSystem and used Moq. Maybe it is useful for someone else.
Thanks for sharing your solution, looks neat to me!
Activity
fgreinacher commentedon Mar 25, 2022
This is currently not possible. As mentioned in the issue you linked these properties should actually be read-only.
Would be cool to support this somehow, but I guess it would be quite some effort. I will probably not find the time for implementing this anytime soon, but would be open for a contribution.
FroggieFrog commentedon Mar 29, 2022
Thank you for your time and your reply.
In the meantime I managed to come up with a workaround.
I made my own
IFileSystemby inheriting fromMockFileSystemand usedMoq. Maybe it is useful for someone else.fgreinacher commentedon Apr 3, 2022
Thanks for sharing your solution, looks neat to me!
github-actions commentedon Mar 9, 2024
This is addressed in release v20.0.28.