Search code examples
c#unit-testingmicrosoft-fakes

Using Microsoft Fakes I get a compile error


I am trying to use Microsoft Fakes to shim the Directory.CreateDirectory(path) call.

When I use the code below it gives me a compile error saying:

The property or indexer 'System.IO.Fakes.ShimDirectory.CreateDirectoryString' cannot be used in this context because it lacks the get accessor

Here is the code I am trying to run that is causing the compile issues:

    _path = @"\\" + TestConfig.Instance.FileShareHost + @"\SharingIsGood";
    using (ShimsContext.Create())
    {
        ShimDirectory.CreateDirectoryString(_path);

        // Directory.CreateDirectory(_path);
        _fatalException = MockRepository.GenerateMock<IHandleFatalExceptions>();
        _filter = "*.txt";
        _fileReader = new FileHandler();
    }

If I remove the ShimDirectory.CreateDirectoryString(_path); line, it compiles fine. So something with that line is strange. I'm new to Microsoft Fakes.

Thanks in advance.


Solution

  • If you're trying to create a fake for ShimDirectory.CreateDirectoryString, your code should look like:

    using (ShimsContext.Create())
    {
        ShimDirectory.CreateDirectoryString = path =>
        {
          // Your fake code here
        }
    
        _path = @"\\" + TestConfig.Instance.FileShareHost + @"\SharingIsGood";
        Directory.CreateDirectory(_path);
    }