流畅的NHibernate映射测试需要永远

时间:2010-01-09 01:53:36

标签: performance unit-testing fluent-nhibernate

我最近开始学习Fluent NH,我在使用这种测试方法时遇到了一些麻烦。它需要永远运行(现在已经运行了十多分钟,没有任何进展迹象......)。

[TestMethod]
public void Entry_IsCorrectlyMapped()
{
    Action<PersistenceSpecification<Entry>> testAction = pspec => pspec
                                               .CheckProperty(e => e.Id, "1")
                                               .VerifyTheMappings();

    TestMapping<Entry>(testAction);
}

使用这个帮助器方法(稍微简化 - 我也有几个try / catch块,以提供更好的错误消息):

public void TestMapping<T>(Action<PersistenceSpecification<T>> testAction) where T : IEntity
{
    using (var session = DependencyFactory.CreateSessionFactory(true).OpenSession())
    {
        testAction(new PersistenceSpecification<T>(session));
    }
}

DependencyFactory.CreateSessionFactory()方法如下所示:

public static ISessionFactory CreateSessionFactory(bool buildSchema)
{
    var cfg = Fluently.Configure()
        .Database(SQLiteConfiguration.Standard.InMemory())
        .Mappings(m => m.FluentMappings.AddFromAssembly(typeof(Entry).Assembly));

    if (buildSchema)
    {
        cfg = cfg.ExposeConfiguration(config => new SchemaExport(config).Create(false, true));
    }
    return cfg.BuildSessionFactory();
}

我已经尝试过调试,但我无法弄清楚瓶颈在哪里。为什么这需要这么长时间?

1 个答案:

答案 0 :(得分:1)

我认为这与您尝试将会话与持久性规范一起使用的方式有关。创建一个类似下面的基础测试类,为您提供会话;如果整个测试花费的时间超过大约3-4秒,那么就会出现问题。

干杯,
Berryl

[TestFixture]
public class UserAutoMappingTests : InMemoryDbTestFixture
{
    private const string _nickName = "berryl";
    private readonly Name _name = new Name("Berryl", "Hesh");
    private const string _email = "bhesh@cox.net";

    protected override PersistenceModel _GetPersistenceModel() { return new UserDomainAutoMapModel().Generate(); }

    [Test]
    public void Persistence_CanSaveAndLoad_User()
    {
        new PersistenceSpecification<User>(_Session)
            .CheckProperty(x => x.NickName, _nickName)
            .CheckProperty(x => x.Email, _email)
            .CheckProperty(x => x.Name, _name)
            .VerifyTheMappings();
    }

}

public abstract class InMemoryDbTestFixture
{
    protected ISession _Session { get; set; }
    protected SessionSource _SessionSource { get; set; }
    protected Configuration _Cfg { get; set; }

    protected abstract PersistenceModel _GetPersistenceModel();
    protected PersistenceModel _persistenceModel;

    [TestFixtureSetUp]
    public void SetUpPersistenceModel()
    {
        _persistenceModel = _GetPersistenceModel();
    }

    [SetUp]
    public void SetUpSession()
    {
        NHibInMemoryDbSession.Init(_persistenceModel); // your own session factory
        _Session = NHibInMemoryDbSession.Session;
        _SessionSource = NHibInMemoryDbSession.SessionSource;
        _Cfg = NHibInMemoryDbSession.Cfg;
    }

    [TearDown]
    public void TearDownSession()
    {
        NHibInMemoryDbSession.TerminateInMemoryDbSession();
        _Session = null;
        _SessionSource = null;
        _Cfg = null;
    }
}

public static class NHibInMemoryDbSession
{
    public static ISession Session { get; private set; }
    public static Configuration Cfg { get; private set; }
    public static SessionSource SessionSource { get; set; }

    public static void Init(PersistenceModel persistenceModel)
    {
        Check.RequireNotNull<PersistenceModel>(persistenceModel);

        var SQLiteCfg = SQLiteConfiguration.Standard.InMemory().ShowSql();
        SQLiteCfg.ProxyFactoryFactory(typeof(ProxyFactoryFactory).AssemblyQualifiedName);

        var fluentCfg = Fluently.Configure().Database(SQLiteCfg).ExposeConfiguration(cfg => { Cfg = cfg; });
        SessionSource = new SessionSource(fluentCfg.BuildConfiguration().Properties, persistenceModel);
        Session = SessionSource.CreateSession();
        SessionSource.BuildSchema(Session, true);
    }

    public static void TerminateInMemoryDbSession()
    {
        Session.Close();
        Session.Dispose();
        Session = null;
        SessionSource = null;
        Cfg = null;
        Check.Ensure(Session == null);
        Check.Ensure(SessionSource == null);
        Check.Ensure(Cfg == null);
    }
}