当前位置:  开发笔记 > 编程语言 > 正文

如何用列表和关系对象测试流畅的NHibernate的PersistenceSpecification.VerifyTheMappings?

如何解决《如何用列表和关系对象测试流畅的NHibernate的PersistenceSpecification.VerifyTheMappings?》经验,为你挑选了1个好方法。

你会如何测试这种情况?

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

但是,当在PersistenceSpecification上使用VerifyTheMappings方法时,我似乎正在走向死胡同.

基本上我有两个类,Recipient和RecipientList.RecipientList类具有到收件人的映射,具有流畅的"HasMany"关系:

public class RecipientListMap : ClassMap
{

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

}

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

private IList _recipients = new List()
        {
            new Recipient { FirstName = "Joe", LastName = "Bloggs", Email = "joe@bloggs.com", IsDeleted = false },
            new Recipient { FirstName = "John", LastName = "Doe", Email = "john@doe.com", IsDeleted = false },
            new Recipient { FirstName = "Jane", LastName = "Smith", Email = "john@smith.com", IsDeleted = false }
        };

        [Test]
        public void Can_Add_RecipientList_To_Database()
        {
            new PersistenceSpecification(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,我不会得到你如何测试这个?

任何帮助,将不胜感激.



1> John_..:

愚蠢的是我在PeristenceSpecification上使用了错误的方法.

我本来应该使用CheckList而不是CheckProperty.

咄!

推荐阅读
农大军乐团_697
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有