我正在向看起来像这样的方法添加测试:
public class ClassUnderTest {
public ClassUnderTest(IMyInterface dependency)
{
this.dependency = dependency;
}
public bool didFault = false;
void MethodUnderTest()
{
var task = dependency.DoSomethingAsync();
if (task != null)
{
task.ContinueWith(t =>
{
if (t.IsFaulted)
{
didFault = true;
}
}
}
}
}
在测试中,我想测试任务出错时的行为。所以我有这个测试:
[TestCase(TestName = "My test")]
public void MyTestCase()
{
Mock<IMyInterface> mock = MockRepository.Create<IMyInterface>();
ClassUnderTest cut = new ClassUnderTest(mock);
mock.Setup(m => m.DoSomethingAsync()).ThrowsAsync(new Exception());
cut.MethodUnderTest();
Assert.IsTrue(cut.didFault);
}
问题在我看来是
ContinueWith
在Assert
之后被称为。我怎么能等
ContinueWith
完成再断言呢?