Permalink
Browse files

refactored the test framework to be async in order to better match th…

…e async driven implementation of HttpClient
  • Loading branch information...
Fresa committed Jul 11, 2018
1 parent 55f0ddd commit 88ece5e4adb4224bbc73514df7622a5202a8bc19
@@ -53,16 +53,22 @@ public class DefaultWebApplicationHostStarter<TApplication> : IWebApplicationHos
public HttpClient Start(ITestConfigurer testConfigurer)
{
void Startup(IApplicationBuilder appBuilder)
if (_testServer == null)
{
appBuilder.Properties[OwinProperties.ExceptionHandler] = OnUnhandledException;
}
void Startup(IApplicationBuilder appBuilder)
{
appBuilder.Properties[OwinProperties.ExceptionHandler] = OnUnhandledException;
}
var webHostBuilder = new WebHostBuilder()
.Configure(Startup)
.UseStartup<TApplication>()
.ConfigureServices(collection => { testConfigurer.Configure(new WebApiServiceContainer(collection)); });
_testServer = new TestServer(webHostBuilder);
var webHostBuilder = new WebHostBuilder()
.Configure(Startup)
.UseStartup<TApplication>()
.ConfigureServices(collection =>
{
testConfigurer.Configure(new WebApiServiceContainer(collection));
});
_testServer = new TestServer(webHostBuilder);
}
return _testServer.CreateClient();
}
@@ -3,24 +3,25 @@
using System.Linq;
using System.Net.Http;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using Test.It.While.Hosting.Your.Web.Application.HostStarters;
namespace Test.It.While.Hosting.Your.Web.Application
{
public abstract class WebApplicationSpecification<TWebApplicationHostStarter> : IUseConfiguration<TWebApplicationHostStarter>
public abstract class WebApplicationSpecificationAsync<TWebApplicationHostStarter> : IDisposable
where TWebApplicationHostStarter : IWebApplicationHostStarter, new()
{
private readonly ConcurrentBag<Exception> _exceptions = new ConcurrentBag<Exception>();
public void SetConfiguration(TWebApplicationHostStarter webHostingFixture)
public async Task SetConfiguration(TWebApplicationHostStarter webHostingFixture)
{
webHostingFixture.OnUnhandledException += RegisterException;
Client = webHostingFixture.Start(new SimpleTestConfigurer(Given));
When();
await WhenAsync();
HandleExceptions();
ThrowOnExceptions();
}
private void RegisterException(Exception exception)
@@ -37,7 +38,7 @@ private void RegisterException(Exception exception)
}
}
private void HandleExceptions()
private void ThrowOnExceptions()
{
if (_exceptions.Any() == false)
{
@@ -66,6 +67,19 @@ private void HandleExceptions()
/// <summary>
/// Application has started and can be called with <see cref="Client"/>.
/// </summary>
protected virtual void When() { }
protected virtual async Task WhenAsync()
{
await Task.CompletedTask;
}
protected virtual void Dispose(bool disposing)
{
Client?.Dispose();
}
public void Dispose()
{
Dispose(true);
}
}
}
@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using Test.It.While.Hosting.Your.Web.Application.HostStarters;
using Test.It.While.Hosting.Your.Web.Application.Utils;
@@ -12,7 +13,7 @@ namespace Test.It.While.Hosting.Your.Web.Application.Core.Tests
public partial class Given_a_foo_bar_application
{
public class When_getting_a_bar_from_foo :
XUnitWindowsServiceSpecification<
XUnitWindowsServiceSpecificationAsync<
DefaultWebApplicationHostStarter<Startup>>
{
private string _idUsedToGet;
@@ -27,10 +28,10 @@ protected override void Given(IServiceContainer configurer)
configurer.RegisterSingleton<IService>(() => service);
}
protected override void When()
protected override async Task WhenAsync()
{
_response = Client.GetAsync("foo/fooId/bar").ConfigureAwait(false).GetAwaiter().GetResult();
_content = _response.Content.ReadAsAsync<BarResponse>().ConfigureAwait(false).GetAwaiter().GetResult();
_response = await Client.GetAsync("foo/fooId/bar");
_content = await _response.Content.ReadAsAsync<BarResponse>();
}
[Fact]

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Test.It.While.Hosting.Your.Web.Application.HostStarters;
using Xunit;
namespace Test.It.While.Hosting.Your.Web.Application.Utils
{
public abstract class XUnitWindowsServiceSpecificationAsync<TWebApplicationStarter> : WebApplicationSpecificationAsync<TWebApplicationStarter>, IAsyncLifetime
where TWebApplicationStarter : class, IWebApplicationHostStarter, new()
{
private TWebApplicationStarter _configuration;
protected async Task SetConfiguration()
{
_configuration = new TWebApplicationStarter();
await SetConfiguration(_configuration);
}
protected override void Dispose(bool disposing)
{
_configuration?.Dispose();
base.Dispose(disposing);
}
public virtual async Task InitializeAsync() => await SetConfiguration();
public Task DisposeAsync() => Task.CompletedTask;
}
}
@@ -1,9 +1,9 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using Test.It.While.Hosting.Your.Web.Application.HostStarters;
using Test.It.While.Hosting.Your.Web.Application.Utils;
using WebApi.Test.Application;
using Xunit;
@@ -12,41 +12,32 @@ namespace Test.It.While.Hosting.Your.Web.Application.Tests
public partial class Given_a_exception_throwing_application
{
public class When_getting_a_bar_from_foo :
XUnitWindowsServiceSpecification<
XUnitWindowsServiceSpecificationAsync<
DefaultWebApplicationHostStarter<
WebApiTestWebApplicationBuilder<
ExceptionThrowingDuringCallApplication>>>
{
private readonly Exception _exceptionCaught;
private HttpResponseMessage _result;
private Exception _exception;
public When_getting_a_bar_from_foo() : base(false)
{
try
{
SetConfiguration();
}
catch (Exception exception)
{
_exceptionCaught = exception;
}
}
public override async Task InitializeAsync() => await base.InitializeAsync()
.ContinueWith(task => _exception = task.Exception?.InnerException, TaskContinuationOptions.OnlyOnFaulted);
protected override void When()
protected override async Task WhenAsync()
{
_result = Client.GetAsync("foo/fooId/bar").ConfigureAwait(false).GetAwaiter().GetResult();
_result = await Client.GetAsync("foo/fooId/bar");
}
[Fact]
public void It_should_have_caught_an_exception()
{
_exceptionCaught.Should().NotBeNull();
_exception.Should().NotBeNull();
}
[Fact]
public void It_should_have_received_consuelas_message()
{
_exceptionCaught.Message.Should().Be("Misser Superman no here.");
_exception.Message.Should().Be("Misser Superman no here.");
}
[Fact]
@@ -1,33 +1,25 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Test.It.While.Hosting.Your.Web.Application.HostStarters;
using Test.It.While.Hosting.Your.Web.Application.Utils;
using WebApi.Test.Application;
using Xunit;
namespace Test.It.While.Hosting.Your.Web.Application.Tests
{
public partial class Given_a_exception_throwing_application
public partial class Given_a_exception_throwing_application2
{
public class When_initializing_the_application :
XUnitWindowsServiceSpecification<
XUnitWindowsServiceSpecificationAsync<
DefaultWebApplicationHostStarter<
WebApiTestWebApplicationBuilder<
ExceptionThrowingDuringSetupApplication>>>
{
private readonly Exception _exceptionCaught;
private Exception _exceptionCaught;
public When_initializing_the_application() : base(false)
{
try
{
SetConfiguration();
}
catch (Exception ex)
{
_exceptionCaught = ex;
}
}
public override async Task InitializeAsync() => await base.InitializeAsync()
.ContinueWith(task => _exceptionCaught = task.Exception?.InnerException,
TaskContinuationOptions.OnlyOnFaulted);
[Fact]
public void It_should_catch_an_exception_during_setup()
@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using Test.It.While.Hosting.Your.Web.Application.HostStarters;
using Test.It.While.Hosting.Your.Web.Application.Utils;
@@ -9,10 +10,10 @@
namespace Test.It.While.Hosting.Your.Web.Application.Tests
{
public partial class Given_a_foo_bar_application
public partial class Given_a_foo_bar_application2
{
public class When_getting_a_bar_from_foo :
XUnitWindowsServiceSpecification<
XUnitWindowsServiceSpecificationAsync<
DefaultWebApplicationHostStarter<
WebApiTestWebApplicationBuilder<
FooBarApplication>>>
@@ -29,10 +30,10 @@ protected override void Given(IServiceContainer configurer)
configurer.RegisterSingleton<IService>(() => service);
}
protected override void When()
protected override async Task WhenAsync()
{
_response = Client.GetAsync("foo/fooId/bar").ConfigureAwait(false).GetAwaiter().GetResult();
_content = _response.Content.ReadAsAsync<BarResponse>().ConfigureAwait(false).GetAwaiter().GetResult();
_response = await Client.GetAsync("foo/fooId/bar");
_content = await _response.Content.ReadAsAsync<BarResponse>();
}
[Fact]

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 88ece5e

Please sign in to comment.