简体   繁体   English

NHibernate的测试实体映射

[英]testing entity mapping of NHibernate

i'm building some basic crud methods for my fluently mapped entities. 我正在为流利的实体绘制一些基本的粗略方法。

i just wanna know if there is a simple way to make the transaction to perform a rollback when i'm running the cud test methods ? 我只是想知道在运行cud测试方法时是否有一种简单的方法可以使事务执行回滚?

the code that being tested perform the commit inside 被测试的代码在内部执行提交

here is a create sample: 这是一个创建示例:

 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;
    }

If you're using xUnit.net, there's an AutoRollback attribute in the contrib project. 如果您使用的是xUnit.net,则contrib项目中有一个AutoRollback属性。 If you're open to using System.Transactions then you can create a new transaction before the session.Open() and session should autoenlist (unless you've set ado.net to not autoenlist). 如果您愿意使用System.Transactions,则可以在session.Open()和session应该自动注册之前创建一个新事务(除非您将ado.net设置为不自动注册)。 Then just abandon at the end. 然后在最后放弃。

I did something similar to this a (long) while 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 have your test extend TransactionalTest. 然后,让您的测试扩展TransactionalTest。 But, I think NUnit, MbUnit and xUnit.net all support transactional tests out of the box or with a contrib project. 但是,我认为NUnit,MbUnit和xUnit.net都支持开箱即用或使用contrib项目进行事务性测试。

检查FUBUMVC Contrib是否有出色的NHibernate CRUD测试方法。

For integration testing, I use a session decorator to auto commit and evict the entity. 对于集成测试,我使用会话装饰器自动提交并逐出实体。 You could adapt it for 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);
    }
}

You can find more details on usage with examples here: http://www.agileatwork.com/integration-testing-with-nhibernate/ 您可以在以下示例中找到有关用法的更多详细信息: http : //www.agileatwork.com/integration-testing-with-nhibernate/

Hmm, I don't think that it is a good idea to let your method create a session and perform transaction handling .... Unless that method is also a service boundary. 嗯,我认为让您的方法创建一个会话并执行事务处理不是一个好主意...。除非该方法也是服务边界。

Suppose you want to create multiple entities in one and the same transaction, how are you going to handle that ? 假设您要在一个事务中创建多个实体,那么您将如何处理呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM