TestServer

ASP.NET Core has the ability to configure a host for testing purposes. Install the TestServer from NuGet:

Microsoft.AspNetCore.TestHost

There's a little setup involved. First, you’ll add an instance of the TestServer. Create a new WebHostBuilder and use the existing Startup class of the web API project. This will wire up the Dependency Injection container that was set up previously. Now, configure the services to set up a new InMemory database.

Take a look at the test here to see the setup required:

[Fact]
public async void ItShouldCallGetSpeakers()
{
// Arrange
var server = new TestServer(new WebHostBuilder()
.UseStartup<Startup>()
.ConfigureServices(services =>
{
services.AddDbContext<SpeakerMeetContext>(o =>
o.UseInMemoryDatabase("SpeakerMeetInMemory"));
}));

var client = server.CreateClient();

// Act
var response = await client.GetAsync("/api/speaker");

// Assert
Assert.NotNull(response);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.222.231