我正在尝试使用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)时,使单元测试有一个回调,以翻转已保存的标志。
测试运行的时间比由于堆栈溢出而中止的测试要运行几分钟。
关于我要怎么做的任何指示?