Web APIの統合テストでは、非同期アクションのテストに問題があります。Web API - インターセプタ - 非同期コントローラのアクションを傍受する
System.InvalidCastExceptionの:キャストできません、私はそれが次の例外で失敗した統合テストを実行していますただし
[HttpGet] [Route("test")] public async Task<ApiResponse> Test() { return await Task.FromResult(new ApiResponse(true)); }
:私はシンプルなコントローラのアクションを作成し、私の簡単なテストで
のオブジェクト 'MoovShack.Api.Mpiel.Shared.ApiModels.ApiResponse'を入力して 'System.Threading.Tasks.Task`1 [MoovShack.Api.Model.Shared.ApiModels.ApiResponse]'と入力します。 Eで ServerApi.IntegrationTests.IdentityControllerTests.d__10.MoveNext() でCastle.Proxies.IIdentityControllerProxy.Test()で :ライン ---終了:\ Devの\ moovshack \ ServerApi.IntegrationTests IdentityControllerTests.cs \例外がスローされた--- System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(AT) NUnit.Framework.Internal.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitForPendingOperationsToComplete(オブジェクト invocationResult)で NUnit.Frameworkで以前の位置からスタックトレースの。 Internal.Commands.TestMethodCommand.RunAsyncTestMethod(TestExecutionContext コンテキスト)
明らかにタスクにラップされたアクションの戻り値の型と一致しない結果が返されているので、どこから来ているのか分かります。
私たちのインターセプタコードのブロック全体が正常に動作:
public void Intercept(IInvocation invocation)
{
// our interceptor implementation ...
// some irrelevant code before this
invocation.ReturnValue = webInvocation.Invoke(_client, invocation.Arguments); // the return value is populated correctly. not wrapped in a task.
}
、待望の結果を返すためにしようとしているとして、テストが失敗した:
[Test]
public async Task GettingAsyncActionResultWillSucceed()
{
var ctl = BuildController(new SameMethodStack("GET"));
var result = await ctl.Test();
Assert.IsTrue(result.Success);
}
をからどこへ行く私は非常にわかりませんよここに。