Skip to content

Instantly share code, notes, and snippets.

@ChuckBryan
Created November 2, 2014 13:42
Show Gist options
  • Save ChuckBryan/7266adbbfd1f9aa21c19 to your computer and use it in GitHub Desktop.
Save ChuckBryan/7266adbbfd1f9aa21c19 to your computer and use it in GitHub Desktop.
SpecsFor SeedDataConfig with Entity Framework
using System;
using System.Data.Entity;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using SpecsFor.Mvc;
using SpecsForDemo.Web.Models;
namespace SpecsForDemo.IntegrationSpecs
{
public class SeedDataConfig : SpecsForMvcConfig
{
public SeedDataConfig()
{
BeforeEachTest(() =>
{
Database.SetInitializer(new TestDbInitializer());
var context = new ApplicationDbContext();
context.Database.Initialize(true);
});
}
}
public class TestDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
protected override void Seed(ApplicationDbContext context)
{
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var user = new ApplicationUser
{
UserName = "real@user.com",
Email = "real@user.com",
};
IdentityResult ir = um.Create(user, "P@ssw0rd1");
Console.WriteLine(ir.Succeeded);
context.SaveChanges();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment