// How can we do this in such a way that you can use StructureMap child containers // for test isolation? public static async Task <IScenarioResult> Scenario(this ISystemUnderTest system, Action <Scenario> configure) { using (var scope = system.Services.GetService <IServiceScopeFactory>().CreateScope()) { var scenario = new Scenario(system, scope); configure(scenario); scenario.Rewind(); try { await system.BeforeEach(scenario.Context).ConfigureAwait(false); await scenario.RunBeforeActions().ConfigureAwait(false); if (scenario.Context.Request.Path == null) { throw new InvalidOperationException("This scenario has no defined url"); } await system.Invoker(scenario.Context).ConfigureAwait(false); scenario.RunAssertions(); await scenario.RunAfterActions().ConfigureAwait(false); } finally { await system.AfterEach(scenario.Context).ConfigureAwait(false); } return(scenario); } }
// SAMPLE: ScenarioSignature /// <summary> /// Define and execute an integration test by running an Http request through /// your ASP.Net Core system /// </summary> /// <param name="system"></param> /// <param name="configure"></param> /// <returns></returns> /// <exception cref="InvalidOperationException"></exception> public static async Task <IScenarioResult> Scenario( this ISystemUnderTest system, Action <Scenario> configure) // ENDSAMPLE { var scenario = new Scenario(system); configure(scenario); scenario.Rewind(); HttpContext context = null; try { context = await system.Invoke(c => { system.BeforeEach(c).GetAwaiter().GetResult(); c.Request.Body.Position = 0; scenario.SetupHttpContext(c); if (c.Request.Path == null) { throw new InvalidOperationException("This scenario has no defined url"); } }); scenario.RunAssertions(context); } finally { await system.AfterEach(context); } if (context.Response.Body.CanSeek) { context.Response.Body.Position = 0; } return(new ScenarioResult(context, system)); }