嗨,我正在尝试了解 moq 框架,但我似乎无法理解一个简单的概念。这是我正在测试的代码:
public interface ILongRunningLibrary {
string RunForALongTime(int interval);
}
public class LongRunningLibrary : ILongRunningLibrary {
public string RunForALongTime(int interval) {
var timeToWait = interval * 1000;
Thread.Sleep(timeToWait);
return string.Format("Waited {0} seconds ", interval);
}
}
这是我的测试:
private Mock<ILongRunningLibrary> _longRunningLibrary;
[SetUp]
public void SetupForTest() {
_longRunningLibrary = new Mock<ILongRunningLibrary>();
}
[Test]
public void TestLongRunningLibrary() {
const int interval = 30;
_longRunningLibrary.Setup(lrl => lrl.RunForALongTime(30))
.Returns("This method has been mocked!");
var result = _longRunningLibrary.Object.RunForALongTime(interval);
Debug.WriteLine("Return from method was '{0}'", result);
}
从我正在阅读的书中了解到,此方法应在 NUnit 控制台上返回“此方法已被模拟”。但在我的情况下,测试通过并且它什么也不返回。
即使我删除:
_longRunningLibrary.Setup(lrl => lrl.RunForALongTime(30))
.Returns("This method has been mocked!");
测试仍然通过,并且在控制台上没有打印任何内容。
为什么在 NUnit 控制台上没有返回“此方法已被模拟”?