2008-12-07 6 views
10

Comment testeriez-vous ce scénario? Je viens juste de commencer à chercher dans NHibernate et à avoir mon premier coup de poker à TDD. Jusqu'à présent, j'ai vraiment apprécié et j'ai utilisé couramment Nhibernate pour ma cartographie des cours.Comment tester les PersistenceSpecification.VerifyTheMappings de fluent-NHibernate avec des listes et des objets relationnels?

Cependant, il semble que je rencontre une impasse quand il s'agit d'utiliser la méthode VerifyTheMappings sur PersistenceSpecification. Essentiellement, j'ai deux classes, Recipient et RecipientList. La classe RecipientList a une cartographie au bénéficiaire d'une relation "hasMany" fluent:

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(); 
    } 

} 

Cependant lorsque j'utilise le code suivant dans mon 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(); 
     } 

L'erreur suivante se produit:

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' 

Je peux voir que l'erreur est parce que je passe dans une liste et la liste retournée est un PersistentGenericBag, jetant ainsi une erreur. Je ne comprends pas comment vous devez tester cela si vous ne pouvez pas passer dans un IList?

Toute aide serait appréciée.

Répondre

9

Eh bien stupidement j'utilisais la mauvaise méthode sur PeristenceSpecification.

J'aurais dû utiliser CheckList et non CheckProperty.

Duh!

+0

Assurez-vous de ne pas faire la même chose avec CheckReference :) –

Questions connexes