-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored Core.Testing to allow tests without Startup class
- Loading branch information
1 parent
ac4dcb5
commit b2a8167
Showing
12 changed files
with
123 additions
and
53 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,55 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Core.Events; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace Core.Testing | ||
{ | ||
public abstract class ApiFixture<TStartup>: IAsyncLifetime where TStartup : class | ||
public abstract class ApiFixture<TStartup>: ApiFixture where TStartup : class | ||
{ | ||
protected readonly TestContext<TStartup> Sut; | ||
public override TestContext CreateTestContext() => | ||
new TestContext<TStartup>(GetConfiguration, SetupServices, SetupWebHostBuilder); | ||
} | ||
|
||
public abstract class ApiFixture: IAsyncLifetime | ||
{ | ||
protected readonly TestContext Sut; | ||
|
||
private HttpClient Client => Sut.Client; | ||
|
||
protected abstract string ApiUrl { get; } | ||
|
||
protected virtual Dictionary<string,string> GetConfiguration(string fixtureName) | ||
=> new Dictionary<string, string>(); | ||
protected virtual Dictionary<string, string> GetConfiguration(string fixtureName) => new(); | ||
|
||
protected virtual Action<IServiceCollection>? SetupServices => null; | ||
|
||
protected virtual Func<IWebHostBuilder, IWebHostBuilder>? SetupWebHostBuilder => null; | ||
|
||
protected ApiFixture() | ||
{ | ||
Sut = new TestContext<TStartup>(GetConfiguration); | ||
Sut = CreateTestContext(); | ||
} | ||
|
||
public virtual TestContext CreateTestContext() => new(GetConfiguration, SetupServices, SetupWebHostBuilder); | ||
|
||
public virtual Task InitializeAsync() => Task.CompletedTask; | ||
|
||
public virtual Task DisposeAsync() => Task.CompletedTask; | ||
|
||
public Task<HttpResponseMessage> GetAsync(string path = "") | ||
public Task<HttpResponseMessage> Get(string path = "") | ||
{ | ||
return Client.GetAsync( | ||
$"{ApiUrl}/{path}" | ||
); | ||
} | ||
|
||
protected Task<HttpResponseMessage> PostAsync(string path, object request) | ||
public Task<HttpResponseMessage> Post(string path, object request) | ||
{ | ||
return Client.PostAsync( | ||
$"{ApiUrl}/{path}", | ||
request.ToJsonStringContent() | ||
); | ||
} | ||
|
||
protected Task<HttpResponseMessage> PostAsync(object request) | ||
public Task<HttpResponseMessage> Post(object request) | ||
{ | ||
return PostAsync(string.Empty, request); | ||
return Post(string.Empty, request); | ||
} | ||
|
||
public IReadOnlyCollection<TEvent> PublishedExternalEventsOfType<TEvent>() where TEvent: IExternalEvent | ||
public IReadOnlyCollection<TEvent> PublishedExternalEventsOfType<TEvent>() where TEvent : IExternalEvent | ||
=> Sut.PublishedExternalEventsOfType<TEvent>(); | ||
|
||
public IReadOnlyCollection<TEvent> PublishedInternalEventsOfType<TEvent>() where TEvent: IEvent | ||
public IReadOnlyCollection<TEvent> PublishedInternalEventsOfType<TEvent>() where TEvent : IEvent | ||
=> Sut.PublishedInternalEventsOfType<TEvent>(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Core.Events.External; | ||
using Core.Requests; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Shipments.Api.Tests.Core; | ||
|
||
namespace Core.Testing | ||
{ | ||
public static class TestWebHostBuilder | ||
{ | ||
public static IWebHostBuilder Create(Dictionary<string, string> configuration, Action<IServiceCollection>? configureServices = null) | ||
{ | ||
var projectDir = Directory.GetCurrentDirectory(); | ||
configureServices ??= _ => { }; | ||
|
||
return new WebHostBuilder() | ||
.UseEnvironment("Tests") | ||
.UseContentRoot(projectDir) | ||
.UseConfiguration(new ConfigurationBuilder() | ||
.SetBasePath(projectDir) | ||
.AddJsonFile("appsettings.json", true) | ||
.AddInMemoryCollection(configuration) | ||
.Build() | ||
) | ||
.ConfigureServices(configureServices); | ||
} | ||
} | ||
} |
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
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
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