NHibernate Test Object Mapping

I am creating some basic crud methods for my fiddles.

I just want to know if there is an easy way to force the transaction to rollback when I run the cud test methods?

the code under test does the commit inside

here is an example of creation:

 public int? Create(IIdentifiable entity)
    {
        int? newId = null;
        using (ISession session = SessionFactoryProvider.NewSession)
        using (ITransaction transaction = session.BeginTransaction())
        {
            object newObj = session.Save(entity);
            newId = (int?)newObj;
            transaction.Commit();
        }

        return (int?)newId;
    }

      

0


a source to share


4 answers


If you are using xUnit.net the contrib project has an AutoRollback attribute. If you are open to using System.Transactions, you can create a new transaction before the session. Open () and session should auto-register (unless you have installed ado.net to not auto-register). Then just give up at the end.

I did something similar to this (a long time ago):



public class TransactionalTest
{
    public TransactionalTest()
    {
        Transaction.Current = new CommittableTransaction();
    }

    ~TransactionalTest()
    {
        if (Transaction.Current != null &&
            Transaction.Current.TransactionInformation.Status !=
            TransactionStatus.Committed)
        {
            Transaction.Current.Rollback();
        }
    }
}

      

Then just ask your test to extend TransactionalTest. But I think NUnit, MbUnit and xUnit.net support transactional tests out of the box or with the Contrib project.

+2


a source


Check out FUBUMVC Contrib for a great NHibernate CRUD testing method.



+1


a source


Hmm, I don't think it is a good idea to let your method create a session and do transaction processing ... Unless that method is also a service boundary.

Suppose you want to create multiple objects in the same transaction, how are you going to handle this?

0


a source


For testing the integration, I use a session decorator to automatically commit and dispatch the object. You can adapt it to your situation:

public class AutoCommitAndEvictSession : SessionDecorator {

    public AutoCommitAndEvictSession(ISession session)
        : base(session) { }

    public override object Save(object obj) {
        object result;
        using (var tx = Session.BeginTransaction()) {
            result = Session.Save(obj);
            tx.Commit();
        }
        Session.Evict(obj);
        return result;
    }

    public override void Update(object obj) {
        CommitAndEvict(base.Update, obj);
    }

    public override void Delete(object obj) {
        CommitAndEvict(base.Delete, obj);
    }

    private void CommitAndEvict(Action<object> action, object entity) {
        using (var tx = Session.BeginTransaction()) {
            action.Invoke(entity);
            tx.Commit();
        }
        Session.Evict(entity);
    }
}

      

More usage information can be found here: http://www.agileatwork.com/integration-testing-with-nhibernate/

0


a source







All Articles