Skip to content

Instantly share code, notes, and snippets.

@scottmuc
Created January 2, 2012 15:05
Show Gist options
  • Save scottmuc/1551024 to your computer and use it in GitHub Desktop.
Save scottmuc/1551024 to your computer and use it in GitHub Desktop.
BLOG: Unit Testing Domain Persistence With NDbUnit
[Fact(Skip = "Only need this to setup the DB")]
public void CreateDatabaseSchemaFromMappingFiles()
{
IWindsorContainer container = new WindsorContainer()
.Install(BinsorScript.FromFile(TestConfiguration.BinsorConfig));
var cfg = container.Resolve<Configuration>();
var schema = new SchemaExport(cfg);
schema.Create(true, true);
}
public class RepositoryTestBase : IDisposable
{
public RepositoryTestBase()
{
InitializeContainer();
}
private static void InitializeContainer()
{
if (IoC.IsInitialized) return;
var container = new WindsorContainer()
.Install(BinsorScript.FromFile(TestConfiguration.BinsorConfig));
IoC.Initialize(container);
}
protected static ISessionManager SessionManager
{
get { return IoC.Resolve<ISessionManager>(); }
}
public void Dispose()
{
IoC.Reset();
}
}
public class TestConfiguration
{
public const string BinsorConfig = @"..\..\config\container.boo";
public const string ConnectionString = @"Data Source=ndbunitdemo.db;Version=3;New=True";
}
public class UserPersistenceTests : UserRepositoryTestContext
{
[Fact]
public void Can_retrieve_user_from_repository()
{
using (SessionManager.OpenSession())
{
var user = Repository.GetById(1);
Assert.Equal(1, user.Id);
}
}
}
public class UserRepositoryTestContext : RepositoryTestBase
{
protected static IUserRepository Repository
{
get { return IoC.Resolve<IUserRepository>(); }
}
public UserRepositoryTestContext()
{
var dbUnitTest = new SqlLiteUnitTest(TestConfiguration.ConnectionString)
{
QuotePrefix = "[",
QuoteSuffix = "]"
};
const string xmlPath = @"..\..\Users\Xml\";
dbUnitTest.ReadXmlSchema(Path.Combine(xmlPath, "UsersDS.xsd"));
dbUnitTest.ReadXml(Path.Combine(xmlPath, "Users.xml"));
dbUnitTest.PerformDbOperation(DbOperationFlag.CleanInsert);
}
}
<?xml version="1.0" encoding="utf-8" ?>
<UsersDS xmlns="http://tempuri.org/UsersDS.xsd">
<Users>
<Id>1</Id>
<Name>User1</Name>
</Users>
<Users>
<Id>2</Id>
<Name>User2</Name>
</Users>
<Roles>
<Id>1</Id>
<Name>Admin</Name>
</Roles>
<Roles>
<Id>2</Id>
<Name>User</Name>
</Roles>
<UserRoles>
<UserId>1</UserId>
<RoleId>1</RoleId>
</UserRoles>
<UserRoles>
<UserId>2</UserId>
<RoleId>2</RoleId>
</UserRoles>
</UsersDS>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment