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

C# 在异步路由中测试取消处理代码?,c#,asynchronous,nancy,C#,Asynchronous,Nancy,在Nancy中测试异步路由时,如何在单元测试中模拟取消条件 从这个角度来看,还没有真正确定如何触发取消。知道这一点可能会有所帮助,但即使不知道这一点,在Browser或Browser.Get上是否有某种方法可以强制所提供的异步取消令牌上的IsCancellationRequested为真 这里有一个非常简化的异步路由,我有一个取消路径和我希望针对取消逻辑的不完整测试 Get["/somethingasync", runAsync: true] = async (parameters, cance

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

从这个角度来看,还没有真正确定如何触发取消。知道这一点可能会有所帮助,但即使不知道这一点,在
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 ());
}
Get[“/somethingasync”,runAsync:true]=async(参数,取消令牌)=>{
等待DoSomethingSlow();
//在做任何其他慢动作之前,先检查取消
if(cancellationToken.IsCancellationRequested){
//TODO:如何在测试中强制采用此路径?
返回“已取消”;
}
等待DoSomethingElseSlow();
返回“成功”;
};
[测试]
公共作废已取消\u请求\u应返回\u已取消()
{
//给定
var browser=新浏览器(使用=>{
使用.Module();
});
//TODO:是什么让下面的'Get'成为
//上述路线中的取消路径?
//什么时候
var result=browser.Get(“/somethingasync”,with=>{
with.HttpRequest();
});
//然后
Assert.AreEqual(“取消”,result.Body.AsString());
}