Mock File IO static class in C #

I'm new to Unit Testing, and I need to make fun of the static File class in the System.IO namespace. I use Rhinomock, which is the best way to do this,

Suppose I need to make fun of File.Exists, File.Delete ...

+6
source share
7 answers

You cannot mock static methods with a Rhino layout. See this question for more information. You can create a facade class to wrap the file system calls that you will use, and then create a mock version of this.

+7
source

See also Vadim SystemWrapper . You can mock with a lot of system classes , but you will need to apply to check your code.

[Test] public void Check_that_FileInfo_methods_Create_and_Delete_are_called() { // Add mock repository. IFileInfoWrap fileInfoRepository = MockRepository.GenerateMock<IFileInfoWrap>(); IFileStreamWrap fileStreamRepository = MockRepository.GenerateMock<IFileStreamWrap>(); // Create expectations fileInfoRepository.Expect(x => x.Create()).Return(fileStreamRepository); fileStreamRepository.Expect(x => x.Close()); fileInfoRepository.Expect(x => x.Delete()); // Test new FileInfoSample().CreateAndDeleteFile(fileInfoRepository); // Verify expectations. fileInfoRepository.VerifyAllExpectations(); fileStreamRepository.VerifyAllExpectations(); } 
+3
source

You should create a wrapper service called IFileService, then you can create a specific one that uses statics for use in your application, and a dummy IFileService that will have fake functions for testing. Make it so that you need to pass the IFileService to the constructor or property for what the class ever uses, so for normal operation you need to go to the IFileService. Remember that in unit testing, you check only that part of the code, and not that it is called as IFileService.

 interface IFileService { bool Exists(string fileName); void Delete(string fileName); } class FileService : IFileService { public bool Exists(string fileName) { return File.Exists(fileName); } public void Delete(string fileName) { File.Delete(fileName); } } class MyRealCode { private IFileService _fileService; public MyRealCode(IFileService fileService) { _fileService = fileService; } void DoStuff() { _fileService.Exists("myfile.txt"); } } 
+2
source

In addition to not being mistaken, Static is easy to answer.

I would like to point out that you should not scoff at the File.IO type because this is not the type you have. You need only the types of layouts that you have .

+1
source

I also used wrapper classes. I use this tool to easily create wrapper classes such as System.IO.File

https://www.nuget.org/packages/Digitrish.WrapperGenerator/

After installing the package, simply enter the following in the package manager console:

Scrapold Wrapper System.IO.File

This will create the IFile.cs interface and one container container of class File.cs. Then you can use IFile for Mock and File.cs for real implementation.

+1
source

Why not use MS Fakes? Wouldn't it just be already supported compared to RhinoMocks, SystemWrapper.Wrapper and SystemWrapper.Interface?

+1
source

You can use the moq framework for this. This is an open source google project that you can download here. Download Moq Framework

For a complete understanding of how to use moq with C #, see the following article.

http://learningcsharpe.blogspot.com/2011/11/how-to-use-moq-library-for-your-unit.html

Thanks, Erandika.

0
source

Source: https://habr.com/ru/post/891500/


All Articles