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

发布于 2024-07-10 11:23:58 字数 2433 浏览 3 评论 0原文

您将如何测试这个场景?

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

然而,当涉及到在 PersistenceSpecification 上使用 VerifyTheMappings 方法时,我似乎陷入了死胡同。

本质上我有两个类,Recipient 和 RecipientList。 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 = "[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();
        }

发生以下错误:

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,你打算如何测试这个?

任何帮助,将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我早已燃尽 2024-07-17 11:23:59

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

我应该使用 CheckList 而不是 CheckProperty。

呃!

Well stupidely I was using the wrong method on PeristenceSpecification.

I should have been using CheckList not CheckProperty.

Duh!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文