如何在不设置 NHibernate 会话的情况下测试软删除事件监听器
我已经根据以下来源覆盖了默认的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在测试代码中对事件侦听器进行子类化,并提供与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.
我相当肯定,如果没有正确配置的会话,您将无法对此进行测试。但是,您可以将 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.