-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use Xunit.DependencyInjection to update tests
fix #41
- Loading branch information
Showing
4 changed files
with
39 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,21 @@ | ||
using System; | ||
using System.Net.Http; | ||
using OpenReservation.Database; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using WeihanLi.Common.Helpers; | ||
using Xunit; | ||
|
||
namespace OpenReservation.API.Test | ||
{ | ||
/// <summary> | ||
/// Shared Context https://xunit.github.io/docs/shared-context.html | ||
/// </summary> | ||
public class APITestFixture : IDisposable | ||
public class APITestFixture | ||
{ | ||
private readonly IWebHost _server; | ||
public IServiceProvider Services { get; } | ||
|
||
public HttpClient Client { get; } | ||
|
||
public APITestFixture() | ||
public APITestFixture(IServiceProvider serviceProvider, HttpClient httpClient) | ||
{ | ||
var baseUrl = $"http://localhost:{NetHelper.GetRandomPort()}"; | ||
_server = WebHost.CreateDefaultBuilder() | ||
.UseUrls(baseUrl) | ||
.UseStartup<TestStartup>() | ||
.Build(); | ||
_server.Start(); | ||
Services = serviceProvider; | ||
|
||
Services = _server.Services; | ||
|
||
Client = new HttpClient(new WeihanLi.Common.Http.NoProxyHttpClientHandler()) | ||
{ | ||
BaseAddress = new Uri($"{baseUrl}") | ||
}; | ||
Client = httpClient; | ||
// Add Api-Version Header | ||
// Client.DefaultRequestHeaders.TryAddWithoutValidation("Api-Version", "1.2"); | ||
|
||
Initialize(); | ||
|
||
Console.WriteLine("test begin"); | ||
} | ||
|
||
/// <summary> | ||
/// TestDataInitialize | ||
/// </summary> | ||
private void Initialize() | ||
{ | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
using (var dbContext = Services.GetRequiredService<ReservationDbContext>()) | ||
{ | ||
if (dbContext.Database.IsInMemory()) | ||
{ | ||
dbContext.Database.EnsureDeleted(); | ||
} | ||
} | ||
|
||
Client.Dispose(); | ||
_server.Dispose(); | ||
|
||
Console.WriteLine("test end"); | ||
} | ||
} | ||
|
||
[CollectionDefinition("APITestCollection")] | ||
public class APITestCollection : ICollectionFixture<APITestFixture> | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters