Created
January 2, 2012 15:05
-
-
Save scottmuc/1551024 to your computer and use it in GitHub Desktop.
BLOG: Unit Testing Domain Persistence With NDbUnit
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
[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); | |
} |
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 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(); | |
} | |
} |
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 class TestConfiguration | |
{ | |
public const string BinsorConfig = @"..\..\config\container.boo"; | |
public const string ConnectionString = @"Data Source=ndbunitdemo.db;Version=3;New=True"; | |
} |
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 class UserPersistenceTests : UserRepositoryTestContext | |
{ | |
[Fact] | |
public void Can_retrieve_user_from_repository() | |
{ | |
using (SessionManager.OpenSession()) | |
{ | |
var user = Repository.GetById(1); | |
Assert.Equal(1, user.Id); | |
} | |
} | |
} |
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 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); | |
} | |
} |
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
<?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