【发布时间】:2018-11-13 19:00:51
【问题描述】:
我有两个完全独立的 Asp.Net Core 系统,这意味着它们位于不同的 Web 域中。尽管如此,它们在 Visual Studio 中仍处于相同的解决方案中。例如,两个 Asp.Net Core 系统都将托管在这两个域上:
https://client-localhost:8080 和 https://api-localhost:8081
客户端应用程序调用 Api 域的许多不同路由以获取数据。
我对 Api 系统进行集成测试(使用 NUnit)没有问题,例如:
// Integration Test for the Api
[TestFixture]
class IntegrationTestShould
{
public TestServer GetTestServerInstance()
{
return new TestServer(new WebHostBuilder()
.UseStartup<TestServerStartup>()
.UseEnvironment("TestInMemoryDb"));
}
[Test]
public async Task ReturnProductDataFromTestInMemoryDb()
{
using (var server = GetTestServerInstance())
{
var client = server.CreateClient();
var response = await client.GetAsync("/products"); // equivalent to: https://api-localhost:8081/products
var responseString = await response.Content.ReadAsStringAsync();
Assert.AreEqual("{ Shows product data coming from the Api }", responseString);
}
}
}
为了对客户端应用程序进行适当的集成测试,我想从客户端应用程序向 Api 进行 Api 调用。
是否可以创建一个单一的测试方法,我可以在其中启动两个测试服务器(客户端和 Api)并通过我的客户端使用 api?
我可以想象,例如,将 Api 测试服务器注入客户端测试服务器,以便我可以通过我的客户端应用程序使用 Api。
是否存在类似以下内容?
// Integration test for the client that relies on the Api
[TestFixture]
class IntegrationTestShould
{
public TestServer GetApiTestServerInstance()
{
return new TestServer(new WebHostBuilder()
.UseStartup<ApiTestServerStartup>()
.UseEnvironment("TestInMemoryDb"));
}
public TestServer GetClientTestServerInstance()
{
return new TestServer(new WebHostBuilder()
.UseStartup<ClientTestServerStartup>()
.UseEnvironment("Development"));
}
[Test]
public async Task ShowProductsFromApiAtClientLevel()
{
using (var apiServer = GetApiTestServerInstance())
using (var clientServer = GetClientTestServerInstance())
{
var client = clientServer.CreateClient(apiServer);
var response = await client.GetAsync("/products"); // equivalent to: https://client-localhost:8080/products which relies on https://api-localhost:8081/products
var responseString = await response.Content.ReadAsStringAsync();
Assert.AreEqual("{ Shows product data coming from the api at client level }",
responseString);
}
}
}
【问题讨论】:
-
Does something like the following exist?你尝试的时候发生了什么?
标签: c# asp.net-core integration-testing