Created
May 28, 2017 09:58
-
-
Save litichevskiydv/dc3cb1f29826c2232fb78f0a5506f158 to your computer and use it in GitHub Desktop.
Base fixture for Web API tests collection
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
public abstract class BaseApiTestsFixture : IDisposable | |
{ | |
protected bool Disposed; | |
public TestServer Server { get; } | |
public Mock<ILogger> MockLogger { get; } | |
public IConfigurationRoot Configuration { get; } | |
public int TimeoutInMilliseconds { get; } | |
public BaseApiTestsFixture(Type startupType) | |
{ | |
MockLogger = MockLoggerExtensions.CreateMockLogger(); | |
var environment = Environment.GetEnvironmentVariable("Hosting:Environment") | |
?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") | |
?? EnvironmentName.Development; | |
var currentDirectory = Path.GetDirectoryName(startupType.GetTypeInfo().Assembly.Location); | |
Server = new TestServer( | |
new WebHostBuilder() | |
.UseContentRoot(currentDirectory) | |
.UseEnvironment(environment) | |
.ConfigureServices(services => services.CaptureCommandLineArguments(new string[0])) | |
.UseMockLogger(MockLogger) | |
.UseStartup(startupType)); | |
Configuration = new ConfigurationBuilder() | |
.SetBasePath(currentDirectory) | |
.AddJsonFile("appsettings.json", false, false) | |
.AddJsonFile($"appsettings.{environment}.json", true, false) | |
.Build(); | |
TimeoutInMilliseconds = Configuration.GetValue<int>("ApiTimeoutInMilliseconds"); | |
} | |
protected virtual void Dispose(bool disposing) | |
{ | |
if(Disposed) | |
return; | |
if (disposing) | |
Server?.Dispose(); | |
Disposed = true; | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
~BaseApiTestsFixture() | |
{ | |
Dispose(false); | |
} | |
} | |
public class BaseApiTestsFixture<TStartup> : BaseApiTestsFixture where TStartup : WebApiBaseStartup | |
{ | |
public BaseApiTestsFixture() : base(typeof(TStartup)) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment