首页 > 解决方案 > Pose Shim:测试无限期运行

问题描述

我正在尝试使用 Pose (请参阅https://github.com/tonerdo/pose)来填充 XDocument 类的实例方法,如下所示:

被测代码:

/// <exception cref="ArgumentException" />
public void Persist<T>(XDocument document) where T : IEntity
{
    if (!HasRegistration(typeof(T))) throw new ArgumentException("oops");

    try
    {
        var filePath = filePaths[typeof(T)];
        document.Save(filePath);
    }
    catch (System.IO.IOException)
    {
        Thread.Sleep(50);
        Persist<T>(document);
        throw;
    }
    catch (Exception)
    {
        throw;
    }
}

单元测试:

[TestMethod()]
public void PersistenceService_Persist_T_Works() 
{
    var saved = false;

    var mockFileSystem = new Mock<IFileSystem>();
    mockFileSystem.Setup(T => T.Directory.GetCurrentDirectory()).Returns("");
    mockFileSystem.Setup(T => T.Directory.GetParent(It.IsAny<string>())).CallBase();
    mockFileSystem.Setup(T => T.Directory.GetParent(It.IsAny<string>()).Parent.FullName).Returns("");

    var persister = new PersistenceService(mockFileSystem.Object);

    var xDoc = new XDocument();

    Shim xDocumentShim = Shim
        .Replace(() => xDoc.Save(Is.A<string>()))
        .With(delegate (XDocument @this, string s) { saved = true; });

    Assert.IsFalse(saved);
    persister.Persist<IEducationEntity>(xDoc);
    Assert.IsTrue(saved);
}

我的意图是,当在被测代码中调用 xDoc.Save(string) 时,对单元测试进行回调以翻转保存的标志。

测试运行了几分钟,然后由于堆栈溢出而中止。

关于我做错了什么的任何指示?

标签: c#unit-testingshim

解决方案


推荐阅读