2010-01-09 55 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