我目前有一个方法调用,需要很长时间才能加载,有时会超时。我想编写一个集成测试,以确保在我尝试更快地进行数据调用之前,在合理的时间内返回我的数据。我该怎么做?
[TestMethod]
public void GetResults_ReturnsDataInReasonableAmountOfTime_Test()
{
var result = _dataAccess.GetListOfResults();
Assert.IsTrue(##How do I Test that result was returned in under 2 seconds?##);
}
答案 0 :(得分:2)
AutoResetEvent
private AutoResetEvent _resetEvent;
[TestInitialize]
public void SetUp()
{
_resetEvent = new AutoResetEvent(false);
}
[TestMethod]
public void GetResults_ReturnsDataInReasonableAmountOfTime_Test()
{
new Thread(() =>
{
// your long method call
_resetEvent.Set();
}).Start();
Assert.IsTrue(_resetEvent.WaitOne(2000));
}