7

I am new to Unit Testing, and I need to mock the File static class in System.IO namespace. I am using Rhinomock, what is the best way to accomplish this,

Lets say I need to mock the File.Exists,File.Delete ...

David MZ
  • 3,648
  • 6
  • 33
  • 50
  • Plenty of examples on this site already, use the search button – ChrisBint Jun 27 '11 at 22:55
  • 1
    possible duplicate of [Mocking Static methods using Rhino.Mocks](http://stackoverflow.com/questions/540239/mocking-static-methods-using-rhino-mocks) – sarnold Jun 27 '11 at 22:57
  • 5
    Sometimes people dont know what to search for, perhaps you could give some examples rather than being completely useless. Just a thought. – CrazyDart Jun 27 '11 at 23:06

7 Answers7

7

You can't mock static methods with Rhino mock. See this question for more info. You could create a facade class to wrap the file system calls you will use and then create a mock version of that.

Community
  • 1
  • 1
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
  • Rather than make your own facade, use one someone else has already written for you: http://systemioabstractions.codeplex.com/ – vcsjones Jun 27 '11 at 23:13
  • Thx this is great, I am going to use this open-source – David MZ Jun 28 '11 at 08:57
  • Not sure I would put my eggs in a basket that only about 50 other people are willing to use... but its your project. – CrazyDart Jun 28 '11 at 16:26
  • @CrazyDart - It's not really all your eggs in a basket. It's open source, keep a copy of the source for yourself if you like it. – vcsjones Jul 08 '11 at 03:47
5

You should create a wrapper service called IFileService, then you can create a concrete that uses the statics for use in your app, and a mock IFileService that will have fake functionality for testing. Make it so you have to pass IFileService into the constructor or a property for what ever class is using it, this way normal operation requires you pass in the IFileService. Remember in Unit Testing you are testing just that part of code not the things its calling to like 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");
    }
}
CrazyDart
  • 3,803
  • 2
  • 23
  • 29
3

See also Vadim's SystemWrapper. You can mock a lot of system classes with it, but you will need to apply the dependency injection pattern to make your code testable.

[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();
}  
Yann Trevin
  • 3,823
  • 1
  • 30
  • 32
1

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

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

Once you installed the package just type the following on Package Manager Console

Scaffold Wrapper System.IO.File

This will generate IFile.cs interface and one concreate wrapper class File.cs. Then you can use the IFile to Mock and File.cs for the real implementation.

Francis
  • 299
  • 3
  • 2
1

Why not using MS Fakes? Wouldn't that be simple, already supported compared to RhinoMocks, SystemWrapper.Wrapper and SystemWrapper.Interface?

pixel
  • 9,653
  • 16
  • 82
  • 149
0

In addition to the you can't mock Static easily answer.

I'd like to point out that you shouldn't be mocking File.IO type, because it is not a type that you own. You should only Mock types that you own.

Gishu
  • 134,492
  • 47
  • 225
  • 308
-1

You can use moq framework for this.It is open source google project which you can download from here. Download Moq framework

To get fully understanding about how to use moq with C# please refer the following article.

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

thanks, Erandika.

  • 1
    Moq cannot test static classes, see http://stackoverflow.com/questions/12580015/how-to-mock-static-methods-in-c-sharp-using-moq-framework – guillem Apr 09 '13 at 21:27