Skip to content

Instantly share code, notes, and snippets.

@alistair
Forked from Antaris/LayeredServices.cs
Last active December 15, 2015 10:58
Show Gist options
  • Save alistair/154653e12f313fbbf655 to your computer and use it in GitHub Desktop.
Save alistair/154653e12f313fbbf655 to your computer and use it in GitHub Desktop.
public abstract class StartupBase
{
public virtual void ConfigureService(IServiceCollection services)
{
// Standard registrations
ConfigureEntityFramework(services);
}
public abstract void ConfigureEntityFramework(IServiceCollection services);
public IConfiguration Configuration { get; set; }
}
public class Startup : StartupBase
{
public override void ConfigureEntityFramework(IServiceCollection services)
{
services.AddEntityFramework()
.AddNpgsql()
.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"]));
}
}
public class TestStartup : StartupBase
{
public override void ConfigureEntityFramework(IServiceCollection services)
{
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<ApplicationDbContext>(options => options.UseInMemoryDatabase());
}
}
[Fact]
public async Task Can_Setup_Whole_Server() {
var server = new TestServer(
TestServer.CreateBuilder()
).UseStartup<TestStartup>());
await Task.Yield();
server.CreateClient().PostAsync("/api/children",
new System.Net.Http.StringContent("{ Name: 'Alistair', FamilyName: 'B', Sex: 'Male', DateOfBirth: '2015-01-01' }",
new System.Text.UTF8Encoding(), "application/json")).Wait();
var response = await server.CreateClient().GetAsync("/api/children");
var body = await response.Content.ReadAsStringAsync();
body.Should().Contain("A");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment