Я занимаюсь разработкой веб-API с использованием ядра ASP.Net. Я делаю комплексное тестирование моего проекта. Я перехожу по этой ссылке, https://koukia.ca/integration-testing-in-asp-net-core-2-0-51d14ede3968. Это мой код.
У меня есть контроллер для тестирования в проекте thegoodyard.api.
namespace thegoodyard.api.Controllers
{
[Produces("application/json")]
[Route("api/category")]
public class CategoryController: Controller
{
[HttpGet("details/{id}")]
public string GetCategory(int id = 0)
{
return "This is the message: " + id.ToString();
}
}
}
Я добавил новый проект модульного тестирования под названием thegoodyard.tests к решению. Я добавил класс TestServerFixture со следующим определением
namespace thegoodyard.tests
{
public class TestServerFixture : IDisposable
{
private readonly TestServer _testServer;
public HttpClient Client { get; }
public TestServerFixture()
{
var builder = new WebHostBuilder()
.UseContentRoot(GetContentRootPath())
.UseEnvironment("Development")
.UseStartup<Startup>(); // Uses Start up class from your API Host project to configure the test server
_testServer = new TestServer(builder);
Client = _testServer.CreateClient();
}
private string GetContentRootPath()
{
var testProjectPath = PlatformServices.Default.Application.ApplicationBasePath;
var relativePathToHostProject = @"..\..\..\..\..\..\thegoodyard.api";
return Path.Combine(testProjectPath, relativePathToHostProject);
}
public void Dispose()
{
Client.Dispose();
_testServer.Dispose();
}
}
}
Затем снова в тестовом проекте я создал новый класс CategoryControllerTests со следующим определением.
namespace thegoodyard.tests
{
public class CategoryControllerTests: IClassFixture<TestServerFixture>
{
private readonly TestServerFixture _fixture;
public CategoryControllerTests(TestServerFixture fixture)
{
_fixture = fixture;
}
[Fact]
public async Task GetCategoryDetai()
{
var response = await _fixture.Client.GetAsync("api/category/details/3");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
bool containMessage = false; //responseString.Contains("This is the message: 3"); - I commented on purpose to make the test fails.
Assert.True(containMessage);
}
}
}
Тогда я сразу выбрал метод теста и нажал «Запустить тесты» в опции, чтобы запустить тест. Но ни один из тестов не был запущен. Это выход.
![enter image description here](https://i.stack.imgur.com/JR150.png)
Чего не хватает в моем коде? Как мне запустить интегрированный тест?