我正在使用 Ayende 在他的博客中使用 NHibernate 进行单元测试的代码,它要求使用 SQLite 进行测试,代码如下:
using System;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Tool.hbm2ddl;
using Environment = NHibernate.Cfg.Environment;
namespace WebAPI.Tests
{
/// <summary>
/// http://ayende.com/blog/3983/nhibernate-unit-testing
/// </summary>
public class InMemoryDatabaseTest : IDisposable
{
private static Configuration _configuration;
private static ISessionFactory _sessionFactory;
protected ISession Session;
public InMemoryDatabaseTest(Assembly assemblyContainingMapping)
{
if (_configuration == null)
{
_configuration = new Configuration()
.SetProperty(Environment.ReleaseConnections, "on_close")
.SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionString, "data source=:memory:")
.AddAssembly(assemblyContainingMapping);
_sessionFactory = _configuration.BuildSessionFactory();
}
Session = _sessionFactory.OpenSession();
new SchemaExport(_configuration).Execute(false, true, false);
}
public void Dispose()
{
Session.Dispose();
}
}
}
我的测试课是:
[TestFixture]
public class FilmsControllerTests : InMemoryDatabaseTest
{
public FilmsControllerTests() : base(typeof(Film).Assembly)
{
}
static readonly Guid ValidFilmId = new Guid("35d3ee0f5ee011bd8822ed629e5");
[Test]
public void CanSaveAndLoadFilm()
{
object id;
using (var tx = Session.BeginTransaction())
{
id = Session.Save(new PdfFilm
{
IsEnabled = true,
Id = ValidFilmId,
PremiumLevel = 1,
PublishDate = new DateTime(20121010),
Pdf = new Document(new byte[byte.Parse(" 162")], new DateTime(20121010)),
Title = "jel",
Authors = "smel",
});
tx.Commit();
}
Session.Clear();
using (var tx = Session.BeginTransaction())
{
var Film = Session.Get<PdfFilm>(id);
Assert.AreEqual(new DateTime(2012, 10, 10), Film.PublishDate);
tx.Commit();
}
}
和型号:
public abstract class Film : Entity<Film>
其中有一个嵌套类:
public class PdfFilm: Film
并在与父级相同的文件中启动它的映射配置:
<subclass name="PdfFilm" discriminator-value="Pdf">
NUnit 单元测试返回:
NHibernate.MappingException : No persister for: Core.PdfFilm
我检查了电影映射文件的属性并将其设置为嵌入式资源 - 任何人都可以确认这是否是最简单的测试方法以及错误消息的含义是什么?
堆栈跟踪:
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)