【发布时间】:2013-08-13 14:25:10
【问题描述】:
我创建了一个自托管的 ASP.NET Web API 实现,并希望它在 SpecFlow 测试中运行。
所以在我的规格中,我有一个步骤来启动 selfhostserver:
var config = new HttpSelfHostConfiguration("http://localhost:9000");
var server = new HttpSelfHostServer(config);
_apiApplication.Start(); //IoC, route-configs etc.
server.OpenAsync().Wait();
var httpClient = new HttpClient();
var response = httpClient.GetAsync(fetchUrl).Result;
GetAsync 调用发生的异常:
Exception : System.AggregateException: One or more errors occurred.
---> System.Net.Http.HttpRequestException: An error occurred while sending the request.
---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
---> System.IO.IOException: Unable to read data from the transport connection
测试运行似乎阻止了在规范完成之前对自托管 API 的任何调用。调试时,我可以调用 url - 但它会挂起,直到测试运行完成。完成后,它会给出很好的响应。
我还创建了一个控制台应用程序,它可以完美地运行此代码,并给出预期的结果。
任何拥有通过 HttpSelfHostServer 进行测试的 SpecFlow 测试套件的人,或者知道如何让自托管 WebApi 在 SpecFlow 套件中工作?
【问题讨论】:
-
你什么时候启动它?它在给定的绑定中吗?
-
@Alski ,我试过把它放几个 SpecFlow 段。在 BeforeScenario 中,在 Given-step 中,然后使用 HttpClient 进行调用,或者只是在单个 When 中将其全部混合。在所有情况下都有相同的效果:/ 我似乎在 MSTest 运行中也得到了相同的效果,所以可能不仅仅是 SpecFlow。
标签: c# asp.net-mvc-4 asp.net-web-api mstest specflow