简体   繁体   中英

Mocking IO operations in Nunit using Typemock

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?

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:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM