如何在不设置 NHibernate 会话的情况下测试软删除事件监听器

发布于 2024-08-14 03:53:42 字数 1002 浏览 8 评论 0原文

我已经根据以下来源覆盖了默认的 NHibernate DefaultDeleteEventListener: http:// nhibernate.info/blog/2008/09/06/soft-deletes.html

所以我有

 protected override void DeleteEntity(
        IEventSource session,
        object entity,
        EntityEntry entityEntry,
        bool isCascadeDeleteEnabled,
        IEntityPersister persister,
        ISet transientEntities)
    {
        if (entity is ISoftDeletable)
        {
            var e = (ISoftDeletable)entity;
            e.DateDeleted = DateTime.Now;
            CascadeBeforeDelete(session, persister, entity, entityEntry, transientEntities);
            CascadeAfterDelete(session, persister, entity, transientEntities);
        }
        else
        {
            base.DeleteEntity(session, entity, entityEntry, isCascadeDeleteEnabled, persister, transientEntities);
        }
    }

如何在不配置 NHIbernate 会话的情况下仅测试这段代码?

I have overridden the default NHibernate DefaultDeleteEventListener according to this source: http://nhibernate.info/blog/2008/09/06/soft-deletes.html

so I have

 protected override void DeleteEntity(
        IEventSource session,
        object entity,
        EntityEntry entityEntry,
        bool isCascadeDeleteEnabled,
        IEntityPersister persister,
        ISet transientEntities)
    {
        if (entity is ISoftDeletable)
        {
            var e = (ISoftDeletable)entity;
            e.DateDeleted = DateTime.Now;
            CascadeBeforeDelete(session, persister, entity, entityEntry, transientEntities);
            CascadeAfterDelete(session, persister, entity, transientEntities);
        }
        else
        {
            base.DeleteEntity(session, entity, entityEntry, isCascadeDeleteEnabled, persister, transientEntities);
        }
    }

How can I test only this piece of code, without configuring an NHIbernate Session?

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

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

发布评论

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

评论(2

一杆小烟枪 2024-08-21 03:53:42

您可以在测试代码中对事件侦听器进行子类化,并提供与DeleteEntity 具有相同签名的公共方法,该方法仅调用DeleteEntity 的受保护基实现。
模拟其他依赖项,调用可测试类中的公共方法并验证 DateDeleted 是否已设置。

You could subclass your event listener in your test code and provide a public method with the same signature as DeleteEntity, which simply calls the protected base implementation of DeleteEntity.
Mock the other dependencies, call the public method in the testable class and verify DateDeleted has been set.

听闻余生 2024-08-21 03:53:42

我相当肯定,如果没有正确配置的会话,您将无法对此进行测试。但是,您可以将 Nhibernate 配置为在测试中使用 SQLite 和一些虚拟数据。

I'm fairly certain you will not be able to test this without a properly configured session. However, you could config Nhibernate to use for instance SQLite with some dummy data in your tests.

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