views:

32

answers:

1

Hi I am writing unit tests for fluent Nhibernate, when I run the test in isloation it passes, but when I run multiple tests. or run the test more than once it starts failing with the message below System.ApplicationException : For property 'Id' expected '1' of type 'System.Int32' but got '2' of type 'System.Int32'

[TextFixture] public void Can_Correctly_Map_Entity() {

    new PersistenceSpecification<UserProfile>(Session)
        .CheckProperty(c => c.Id, 1)
        .CheckProperty(c => c.UserName, "user")
        .CheckProperty(c => c.Address1, "Address1")
        .CheckProperty(c => c.Address2, "Address2")

}

I got a solution that seems to make sense here http://stackoverflow.com/questions/3188311/fluent-nhibernate-system-applicationexception-for-property-id-expected-1-of, but Im not sure how to use this as the explanation is vague

A: 

Are you testing against an in-memory database, or a physical one? I'd suggest testing your mappings using an in-memory database so that you can isolate these tests to only testing the mappings. If you use an in-memory database, you can place the FluentConfiguration in the [TestInitialize] (MSTest) or [SetUp] (NUnit) method, and the db will be created from scratch each time. Here's an example:

[TestInitialize] 
public void PersistenceSpecificationTest() 
{ 
    var cfg = Fluently.Configure() 
        .Database(SQLiteConfiguration.Standard.InMemory().UseReflectionOptimizer()) 
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserProfile>()) 
        .BuildConfiguration(); 

    _session = cfg.BuildSessionFactory().OpenSession(); 
    new SchemaExport(cfg).Execute(false, true, false, _session.Connection, null); 
} 

Then your test should work fine each time you run:

[TestMethod]
pulic void CanMapUserProfile()
{
    new PersistenceSpecification<UserProfile>(_session)    
        .CheckProperty(c => c.Id, 1)    
        .CheckProperty(c => c.UserName, "user")    
        .CheckProperty(c => c.Address1, "Address1")    
        .CheckProperty(c => c.Address2, "Address2") 
}

You'll need to use SQLite in this scenario, along with the System.Data.SQLite DLL, which you can find here: http://sqlite.phxsoftware.com/

Hope that helps.

Brandon Satrom