2010-01-09 59 views
0

我最近开始学习Fluent NH,并且在测试方法上遇到了一些麻烦。它需要永远运行(现在已经运行了十多分钟,并没有进展的迹象......)。流利的NHibernate映射测试需要永久

[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(); 
} 

我试过调试过,但是我找不出瓶颈在哪里。为什么这需要这么长时间?

+0

有多长 '长'?范围内有多少映射? – 2010-01-09 01:56:47

+0

我说了十分钟,但现在已经过了半个多小时 - 测试跑步者仍然说“正在进行......”。到目前为止,只有两种映射 - 两者都非常简单。 – 2010-01-09 02:13:44

回答

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 = "[email protected]"; 

    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); 
    } 
} 
+0

感谢您的回复!我冒昧地编辑你的回复,以便代码看起来很好。我无法弄清楚类NHibernateInMemboryDbSession来自哪里。它是一些程序集的一部分,还是我必须自己定义它? – 2010-01-13 02:28:02

+0

我曾假设你会创建它,但我在上面添加它以向你展示它会是什么样子。欢呼声 – Berryl 2010-01-13 17:22:07

+0

Ho Tomas。这是如何解决你的? – Berryl 2010-01-16 16:06:24