Skip to content

Commit

Permalink
use Test Server to serve request instead of a real web server in api …
Browse files Browse the repository at this point in the history
…test
  • Loading branch information
WeihanLi committed Nov 7, 2020
1 parent b429358 commit e683065
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
1 change: 1 addition & 0 deletions OpenReservation.API.Test/OpenReservation.API.Test.csproj
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="$(DotNetCorePackageVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="$(DotNetCorePackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="$(DotNetCorePackageVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
Expand Down
17 changes: 6 additions & 11 deletions OpenReservation.API.Test/Startup.cs
@@ -1,8 +1,7 @@
using System;
using System.Net.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -16,7 +15,6 @@
using OpenReservation.Events;
using OpenReservation.Services;
using WeihanLi.Common.Event;
using WeihanLi.Common.Helpers;
using WeihanLi.Redis;
using WeihanLi.Web.Authentication;
using WeihanLi.Web.Authentication.HeaderAuthentication;
Expand All @@ -27,19 +25,16 @@ public class Startup
{
public void ConfigureHost(IHostBuilder hostBuilder)
{
var baseUrl = $"http://localhost:{NetHelper.GetRandomPort()}";

hostBuilder
.ConfigureWebHostDefaults(builder =>
{
builder.UseUrls(baseUrl);
builder.UseTestServer();
builder.ConfigureServices((context, services) =>
{
services.TryAddSingleton(sp =>
sp.GetRequiredService<IHost>().GetTestClient()
);
services.TryAddSingleton<APITestFixture>();
services.TryAddSingleton(new HttpClient()
{
BaseAddress = new Uri(baseUrl)
});
ConfigureServices(services, context.Configuration);
});
Expand Down

0 comments on commit e683065

Please sign in to comment.