c# - 如何使用列表和关系对象测试 fluent-NHibernate 的 PersistenceSpecification.VerifyTheMappings?

标签 c# .net fluent-nhibernate nunit

你会如何测试这个场景?

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

但是,当谈到在 PersistenceSpecification 上使用 VerifyTheMappings 方法时,我似乎陷入了死胡同。

基本上我有两个类,Recipient 和 RecipientList。 RecipientList 类有一个到 Recipient 的映射,具有流畅的“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", IsDeleted = false },
            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'

我可以看到错误是因为我传入了一个列表,而返回的列表是一个 PersistentGenericBag,因此抛出了错误。我不明白如果您不能只传入一个 IList,您应该如何测试它?

如有任何帮助,我们将不胜感激。

最佳答案

很愚蠢,我在 PeristenceSpecification 上使用了错误的方法。

我应该一直使用 CheckList 而不是 CheckProperty。

呃!

关于c# - 如何使用列表和关系对象测试 fluent-NHibernate 的 PersistenceSpecification.VerifyTheMappings?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/347602/

相关文章:

c# - 工作流是否适合在基于 Web 的应用程序中创建动态工作流?

c# - NHibernate AliasToBean 转换器抛出异常,然后 QueryOver 别名是私有(private)字段

asp.net - NHibernate 和 ASP.NET 成员资格

nhibernate - Fluent nhibernate 映射嵌套属性 "cannot find getter"

c# - 具有元组键的字典比嵌套字典慢。为什么?

c# - 尝试删除多对多关系中两个对象之间的连接时出现的问题

c# - C# WPF 代码隐藏中的按钮处理程序

c# - C# 中的正确函数?

c# - 从用户控件访问父窗口

c# - 我可以访问我刚在 C# 中创建的 HTML 控件的高度/宽度吗?