views:

89

answers:

2

I have overridden the default NHibernate DefaultDeleteEventListener according to this source: http://nhforge.org/blogs/nhibernate/archive/2008/09/06/soft-deletes.aspx

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?

A: 

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.

UpTheCreek
+1  A: 

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.

mattk