在Nancy中测试异步路由时,如何在单元测试中模拟取消条件?
在查看此question on the source of Nancy's cancellation tokens时,尚未确定如何触发取消。知道这可能对此有所帮助,但即使没有这方面的知识,Browser
或Browser.Get
上有一些方法可以强制IsCancellationRequested
在提供的异步取消令牌上成立吗?
这是一个非常简化的异步路由,其中我有一个取消路径和不完整的测试我希望将该取消逻辑作为目标。
Get["/somethingasync", runAsync: true] = async (parameters, cancellationToken) => {
await DoSomethingSlow();
// Check for cancellation before doing anything else slow
if (cancellationToken.IsCancellationRequested) {
// TODO: How do I force this path to be taken in a test?
return "cancelled";
}
await DoSomethingElseSlow();
return "success";
};
[Test]
public void Cancelled_request_should_return_cancelled ()
{
// Given
var browser = new Browser (with => {
with.Module<SomeModuleContainingAboveRoute> ();
});
// TODO: What goes here to make the following `Get` take the
// cancellation path in the above route?
// When
var result = browser.Get ("/somethingasync", with => {
with.HttpRequest ();
});
// Then
Assert.AreEqual ("cancelled", result.Body.AsString ());
}