在异步Nancy路由中测试取消处理代码?

时间:2016-03-16 15:36:30

标签: c# asynchronous nancy

在Nancy中测试异步路由时,如何在单元测试中模拟取消条件?

在查看此question on the source of Nancy's cancellation tokens时,尚未确定如何触发取消。知道这可能对此有所帮助,但即使没有这方面的知识,BrowserBrowser.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 ());
}

0 个答案:

没有答案