C# · 12月 30, 2021

c# – 如何测试流畅的NHibernate的PersistenceSpecification.VerifyTheMappings与列表和关系对象?

你将如何测试这种情况?

我刚刚开始研究NHibernate,并在TDD上进行了第一个bash.到目前为止,我非常喜欢它,并且一直在使用流畅的Nhibernate来映射类.

但是,当使用“验证规则”中的VerifyTheMappings方法时,我似乎正在陷入僵局.

本质上我有两个类,即收件人和收件人列表. RecipientList类有一个流畅的“HasMany”关系的映射到收件人:

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

但是当我在测试中使用以下代码时:

private IList<Recipient> _recipients = new List<Recipient>() { new Recipient { FirstName = “Joe”,LastName = “Bloggs”,Email = “joe@bloggs.com”,IsDeleted = false },new Recipient { FirstName = “John”,LastName = “Doe”,Email = “john@doe.com”,new Recipient { FirstName = “Jane”,LastName = “Smith”,Email = “john@smith.com”,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(); }

发生以下错误:

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’

我可以看到错误是因为我传递一个List,而返回的列表是一个PersistentGenericBag,因此会抛出一个错误.我不知道如果你不能通过一个IList,你会怎么想呢测试一下?

任何帮助将不胜感激.

解决方法 嗯我愚蠢地使用了错误的方法来进行PeristenceSpecification.

我应该使用CheckList不是CheckProperty.

咄!