views:

2208

answers:

1

How would you test this scenario?

I've just started looking into NHibernate and having my first bash at TDD. So far I've really enjoyed it and have been using fluent-Nhibernate for my mapping of the classes.

However I seem to be hitting a dead end when it comes to using the VerifyTheMappings method on PersistenceSpecification.

Essentially I have two classes, Recipient and RecipientList. The RecipientList class has a mapping to the Recipient with a fluent "HasMany" relationship:

public class RecipientListMap : ClassMap<RecipientList>
{

    public RecipientListMap()
    {
        Id(x => x.ID);
        Map(x => x.ApplicationID);
        Map(x => x.Name);
        Map(x => x.IsDeleted);
        HasMany<Recipient>(x => x.Recipients).WithKeyColumn("RecipientListID").AsList().LazyLoad();
    }

}

However when I use the following code in my test:

private IList<Recipient> _recipients = new List<Recipient>()
        {
            new Recipient { FirstName = "Joe", LastName = "Bloggs", Email = "[email protected]", IsDeleted = false },
            new Recipient { FirstName = "John", LastName = "Doe", Email = "[email protected]", IsDeleted = false },
            new Recipient { FirstName = "Jane", LastName = "Smith", Email = "[email protected]", IsDeleted = false }
        };

        [Test]
        public void Can_Add_RecipientList_To_Database()
        {
            new PersistenceSpecification<RecipientList>(Session)
                .CheckProperty(x => x.Name, "My List")
                .CheckProperty(x => x.Columns, "My columns")
                .CheckProperty(x => x.IsDeleted, false)
                .CheckProperty(x => x.ApplicationID, Guid.NewGuid())
                .CheckProperty(x => x.Recipients, _recipients)
                .VerifyTheMappings();
        }

The following error occurs:

failed: System.ApplicationException : Expected 'System.Collections.Generic.List`1[Project.Data.Domains.Recipients.Recipient]' but got 'NHibernate.Collection.Generic.PersistentGenericBag`1[Project.Data.Domains.Recipients.Recipient]' for Property 'Recipients'

I can see that the error is because I am passing in a List and the list returned is a PersistentGenericBag, therefore throwing an error. I don't get how you are suppose test this though if you can't just pass in an IList?

Any help would be appreciated.

+3  A: 

Well stupidely I was using the wrong method on PeristenceSpecification.

I should have been using CheckList not CheckProperty.

Duh!

John_
Be sure not to do the same with CheckReference :)
John_