Skip to content

Instantly share code, notes, and snippets.

@alistair
Last active December 15, 2015 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alistair/300bd18adf58b4e34be0 to your computer and use it in GitHub Desktop.
Save alistair/300bd18adf58b4e34be0 to your computer and use it in GitHub Desktop.
namespace blackdunes.chronicles
{
public class Startup
{
private readonly IHostingEnvironment env;
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
this.env = env;
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add EntityFramework, but if my tests have already defined it, then use what they have defined instead.
services.AddEntityFramework()
.AddNpgsql()
.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddMvc();
services.AddScoped<IChronicleRepository, ChronicleRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory, ContextSeedData data)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseStaticFiles();
Mapper.Initialize(config =>
{
config.CreateMap<Child, ChildViewModel>().ReverseMap();
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
[Fact]
public async Task Can_Setup_Whole_Server() {
var server = new TestServer(
TestServer.CreateBuilder()
.UseEnvironment("Testing")
.UseServices(services => {
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<ApplicationDbContext>(options =>
{
options.UseInMemoryDatabase(); options.Lock();
});
}).UseStartup<Startup>());
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