【问题标题】:Mocking IO operations in Nunit using Typemock使用 Typemock 在 Nunit 中模拟 IO 操作
【发布时间】:2013-07-01 13:27:51
【问题描述】:

我有以下方法。

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;
}

我尝试在我的测试用例中使用以下内容。

[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);
} 

它抛出了以下 typemock 异常。

TypeMock.TypeMockException : * 在录制块中没有找到方法调用。请检查: * 您是否试图伪造字段而不是属性? * 您是否在尝试伪造不受支持的 mscorlib 类型?

有没有办法解决这个问题?

【问题讨论】:

  • 但他们没有提到如何使用 Typemock 实现这一目标
  • 你能分享你整个单元测试的代码吗?
  • 请始终在问题本身而不是在 cmets 中为您的问题添加附加信息或说明。这将有助于未来的用户/观众理解问题。
  • @RyanGates:根据您的评论编辑

标签: c# nunit typemock typemock-isolator nunit-2.5.9


【解决方案1】:

如果您查看您得到的异常的最后一个问题,

* 您是否在尝试伪造不受支持的 mscorlib 类型?

异常中后面其实有一个网址,写着

在此处查看支持的类型:http://www.typemock.com/mscorlib-types

当你去那里时,你会注意到:

此列表中明显缺少的是 System.IO.Directory 类,在撰写本文时,TypeMock 似乎不受支持。这是导致您的错误的行。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多