/// <summary> /// Used to start the fixture within a <see cref="ComposedFixture"/>. /// </summary> /// <typeparam name="TStartup">The startup class for the service.</typeparam> /// <param name="hostConfigurator">Optional action providing for customization of the hosting environment.</param> /// <param name="port">The port where the server will listen or zero to allow the operating system to select a free port.</param> /// <param name="logWriter">Optionally specifies a test output writer.</param> /// <param name="logLevel">Optionally specifies the log level. This defaults to <see cref="LogLevel.None"/>.</param> /// <remarks> /// <para> /// You can capture ASP.NET and service logs into your unit test logs by passing <paramref name="logWriter"/> as /// non-null and <paramref name="logLevel"/> as something other than <see cref="LogLevel.None"/>. You'll need /// to obtain a <see cref="ITestOutputHelper"/> instance from Xunit via dependency injection by adding a parameter /// to your test constructor and then creating a <see cref="TestOutputWriter"/> from it, like: /// </para> /// <code language="c#"> /// public class MyTest : IClassFixture<AspNetFixture> /// { /// private AspNetFixture fixture; /// private TestAspNetFixtureClient client; /// private TestOutputWriter testWriter; /// /// public Test_EndToEnd(AspNetFixture fixture, ITestOutputHelper outputHelper) /// { /// this.fixture = fixture; /// this.testWriter = new TestOutputWriter(outputHelper); /// /// fixture.Start<Startup>(logWriter: testWriter, logLevel: Neon.Diagnostics.LogLevel.Debug); /// /// client = new TestAspNetFixtureClient() /// { /// BaseAddress = fixture.BaseAddress /// }; /// } /// } /// </code> /// </remarks> public void StartAsComposed <TStartup>(Action <IWebHostBuilder> hostConfigurator = null, int port = 0, TestOutputWriter logWriter = null, LogLevel logLevel = LogLevel.None) where TStartup : class { this.hostConfigurator = hostConfigurator; this.logWriter = logWriter; this.logLevel = logLevel; base.CheckWithinAction(); if (IsRunning) { return; } StartServer <TStartup>(port); // Get the address where the server is listening and create the client. JsonClient = new JsonClient() { BaseAddress = new Uri(WebHost.ServerFeatures.Get <IServerAddressesFeature>().Addresses.OfType <string>().FirstOrDefault()) }; IsRunning = true; }
/// <summary> /// <para> /// Starts the ASP.NET service using the default controller factory. /// </para> /// <note> /// You'll need to call <see cref="StartAsComposed{TStartup}(Action{IWebHostBuilder}, int, TestOutputWriter, LogLevel)"/> /// instead when this fixture is being added to a <see cref="ComposedFixture"/>. /// </note> /// </summary> /// <typeparam name="TStartup">The startup class for the service.</typeparam> /// <param name="hostConfigurator">Optional action providing for customization of the hosting environment.</param> /// <param name="port">The port where the server will listen or zero to allow the operating system to select a free port.</param> /// <param name="logWriter">Optionally specifies a test output writer.</param> /// <param name="logLevel">Optionally specifies the log level. This defaults to <see cref="LogLevel.None"/>.</param> /// <remarks> /// <para> /// You can capture ASP.NET and service logs into your unit test logs by passing <paramref name="logWriter"/> as /// non-null and <paramref name="logLevel"/> as something other than <see cref="LogLevel.None"/>. You'll need /// to obtain a <see cref="ITestOutputHelper"/> instance from Xunit via dependency injection by adding a parameter /// to your test constructor and then creating a <see cref="TestOutputWriter"/> from it, like: /// </para> /// <code language="c#"> /// public class MyTest : IClassFixture<AspNetFixture> /// { /// private AspNetFixture fixture; /// private TestAspNetFixtureClient client; /// private TestOutputWriter testWriter; /// /// public Test_EndToEnd(AspNetFixture fixture, ITestOutputHelper outputHelper) /// { /// this.fixture = fixture; /// this.testWriter = new TestOutputWriter(outputHelper); /// /// fixture.Start<Startup>(logWriter: testWriter, logLevel: Neon.Diagnostics.LogLevel.Debug); /// /// client = new TestAspNetFixtureClient() /// { /// BaseAddress = fixture.BaseAddress /// }; /// } /// } /// </code> /// </remarks> public void Start <TStartup>(Action <IWebHostBuilder> hostConfigurator = null, int port = 0, TestOutputWriter logWriter = null, LogLevel logLevel = LogLevel.None) where TStartup : class { base.CheckDisposed(); base.Start( () => { StartAsComposed <TStartup>(hostConfigurator, port, logWriter, logLevel); }); }