3
我正在通過DDD項目DDD Sample.Net的開源示例的一些代碼,並遇到了一個'有趣的'SQLite基礎測試夾具(鏈接中的完整代碼)。sqlite單元測試與db文件
而我通常使用SQLite的連接字符串:
"Data Source=:memory:;Version=3;New=True;Pooling=True;Max Pool Size=1")
筆者使用與它DBFILE(他建立/刪除每個測試):
"Data Source={0};Version=3;New=True;", DatabaseFile)
什麼這樣做的好處?
乾杯,
Berryl
EDIT
我懷疑原因在可以是一個優點是由於代碼在該類中(見下文)的平衡表明作者既不是不復雜的,也沒有幼稚。這樣做的好處不太可能是性能。我猜測它可能使SQLite的整個測試運行更可靠,但是這就是爲什麼我問: - )
protected IDisposable Scope(bool transactional)
{
return new ScopeImpl(SessionFactory, transactional);
}
protected IDisposable Scope(bool transactional, string description)
{
Console.WriteLine(description);
return Scope(transactional);
}
private class ScopeImpl : IDisposable
{
private readonly ISessionFactory _sessionFactory;
public ScopeImpl(ISessionFactory sessionFactory, bool transactional)
{
_sessionFactory = sessionFactory;
ISession session = _sessionFactory.OpenSession();
if (transactional)
{
session.BeginTransaction();
}
CurrentSessionContext.Bind(session);
}
public void Dispose()
{
ISession session = CurrentSessionContext.Unbind(_sessionFactory);
if (!IsInExceptionContext())
{
if (session.Transaction != null)
{
session.Transaction.Commit();
session.Transaction.Dispose();
}
}
session.Close();
}
/// <summary>
/// Checks if current code is running in finally block ater throwing exception.
/// </summary>
private static Boolean IsInExceptionContext()
{
return Marshal.GetExceptionPointers() != IntPtr.Zero || Marshal.GetExceptionCode() != 0;
}
這可能是作者沒有意識到你可以在內存中運行SQLite。沒有真正的優勢,事實上,我會願意說這是一個不利的,必須寫設置/拆解。 – Phill
我認爲任何實際訪問數據庫的測試(特別是每次運行測試時創建一個新數據庫)都是集成測試,而不是單元測試。 –