text stringlengths 0 13.4k |
|---|
Write an integration test |
There are a few things that need to be configured on the test server |
before each test. Instead of cluttering the test with this setup code, you |
can keep this setup in a separate class. Create a new class called |
TestFixture : |
107 |
Integration testing |
AspNetCoreTodo.IntegrationTests/TestFixture.cs |
using System; |
using System.Collections.Generic; |
using System.IO; |
using System.Net.Http; |
using Microsoft.AspNetCore.Hosting; |
using Microsoft.AspNetCore.TestHost; |
using Microsoft.Extensions.Configuration; |
namespace AspNetCoreTodo.IntegrationTests |
{ |
public class TestFixture : IDisposable |
{ |
private readonly TestServer _server; |
public HttpClient Client { get; } |
public TestFixture() |
{ |
var builder = new WebHostBuilder() |
.UseStartup<AspNetCoreTodo.Startup>() |
.ConfigureAppConfiguration((context, config) => |
{ |
config.SetBasePath(Path.Combine( |
Directory.GetCurrentDirectory(), |
"..\\..\\..\\..\\AspNetCoreTodo")); |
config.AddJsonFile("appsettings.json"); |
}); |
_server = new TestServer(builder); |
Client = _server.CreateClient(); |
Client.BaseAddress = new Uri("http://localhost:8888"); |
} |
public void Dispose() |
{ |
Client.Dispose(); |
_server.Dispose(); |
} |
} |
} |
108 |
Integration testing |
This class takes care of setting up a TestServer , and will help keep the |
tests themselves clean and tidy. |
Now you're (really) ready to write an integration test. Create a new class |
called TodoRouteShould : |
AspNetCoreTodo.IntegrationTests/TodoRouteShould.cs |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.