Mocking IO operations in Nunit using Typemock

708 Views Asked by At

I have the following method.

public string CreateFile(string path, string fileName)
{
    string fullPath = path + "ExportedFiles\\";
    if (Directory.Exists(fullPath))
    {
        DirectoryInfo dir = new DirectoryInfo(fullPath);
        foreach (FileInfo fi in dir.GetFiles())
        {
            fi.Delete();
        }
    }
    else
    {
        Directory.CreateDirectory(fullPath);
    }
    string filePath = fullPath + fileName;
    return filePath;
}

I attempted to used the following in my test case.

[Test, Isolated] 
public void TestCreateFileIfPathExists() 
{ 
    Isolate.WhenCalled(() => System.IO.Directory.Exists("gsgs")).WillReturn(true);
    Isolate.WhenCalled(() => testMyClass.CreateFile(@"D:\testFolder\","TestFile")).CallOriginal(); 
    string result = testMyClass.CreateFile(@"D:\testFolder\", "TestFile");
    Assert.AreEqual("D:\\testFolder\\ExportedFiles\\TestFile", result);
} 

It's throwing the following typemock exception.

TypeMock.TypeMockException : * No method calls found in recording block. Please check: * Are you trying to fake a field instead of a property? * Are you are trying to fake an unsupported mscorlib type?

Is there any way to solve this?

1

There are 1 best solutions below

1
On

If you look at the last question of the exception that you are getting,

* Are you are trying to fake an unsupported mscorlib type?

there is actually a web address after that in the exception, which reads

See supported types here: http://www.typemock.com/mscorlib-types

When you go there, you will notice:

enter image description here

Notably absent from this list is the System.IO.Directory class, which appears to not be supported TypeMock as of this writing. This is the line that is causing your error.