如何为 Fluent nHibernate 约定创建测试?
我正在为 Fluent nHibernate 创建一套一组约定。我创建了一些约定,例如主键、外键和多对多表。
我希望能够使用内存数据库测试这些约定,看看我是否正确编码了这些约定。
目前,我正在使用 SQlite 内存数据库设置 nHibernate,如下所示:
Configuration configuration = null;
var factory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<DataLayer>();
m.FluentMappings.Conventions.AddFromAssemblyOf<DataLayer>();
})
.ExposeConfiguration((c) => configuration = c)
.BuildSessionFactory();
var session = factory.OpenSession();
var export = new SchemaExport(configuration);
export.Execute(true, true, false, session.Connection, null);
我创建一个测试用例,但我不知道测试命名约定。如何使用 Visual Studio 测试项目实现一些约定的单元测试?
I am creating a set of conventions for Fluent nHibernate. I create a few conventions, like primary key, foreign key and many-to-many table.
I would like to be able to test out these conventions with an in memory database to see if I coded these conventions correctly.
Currently, I am setting up nHibernate using an SQlite in-memory database like this:
Configuration configuration = null;
var factory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<DataLayer>();
m.FluentMappings.Conventions.AddFromAssemblyOf<DataLayer>();
})
.ExposeConfiguration((c) => configuration = c)
.BuildSessionFactory();
var session = factory.OpenSession();
var export = new SchemaExport(configuration);
export.Execute(true, true, false, session.Connection, null);
I create a test case, but I don't know test a naming convention. How can I achieve some unit tests on a conventions using a Visual Studio test project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我所做的不是针对数据库进行测试,而是只是测试生成的映射。例如,我有一个约定,规定所有外键都像 ID 一样编写,并且我对其进行测试(我使用 xunit 而不是 mstest,但希望您能理解这个概念......):
顺便说一句。对数据库进行测试不会太困难,只需尝试从 TestClass2 中选择一些内容并确保不会引发异常...但我认为我展示的方法更简单,并且我相信 FluentNhibernate 可以生成正确的 NHibernate映射,NHibernate 可以为我生成正确的查询。
What I did is not testing against the database, but just test the generated mapping. So for example I have a convention that says that all foreign keys are written like ID and I test it like (I use xunit and not mstest, but hopefully you can get the concept...):
Btw. it wouldn't be too difficult to test against a DB, just try to select something from TestClass2 and make sure no exception is thrown... but I think the way I showed is easier and I trust that when FluentNhibernate can generate the correct NHibernate mapping, NHibernate can generate the correct queries for me.