2010-11-17 36 views
2

好吧,这是我的非常非常流畅的hibernate项目。我在hibernate和nhibernate中有很少的经验。如何用流利的nhibernate(schemaexport)测试生成表?在asp.net上下文

由于这是一个网络应用程序项目,所以这个上下文对我来说是全新的。 所以我有我的webapp项目与大多数在网上找到的流利nhibernate。 所以我有这样的实体:

namespace myproject.model 
{ 
    public class Request 
    { 
    public virtual string Id { get; private set; } 
    public virtual Route route { get; set; } 
    public virtual int code { get; set; } 

    } 
} 

namespace myproject.model 
{ 
    public class Route 
    { 
    public virtual string Id { get; private set; } 
    public virtual string client_id { get; set; } 
    public virtual IList<Request> requests { get; set; } 

    public Route() 
    { 
     requests = new List<Request>(); 
    } 

    } 

} 

//Mapping are like this.will only post one 
namespace myproject.mappings 
{ 
public class RequestMap : ClassMap<Request> 
{ 
    public RequestMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.short_code); 
     References(x => x.route); 
    } 
    } 
} 

//NhibernateSessionPerRequest 
namespace myproject.Boostrap 
{ 
    public class NhibernateSessionPerRequest : IHttpModule 
    { 
    private static readonly ISessionFactory _sessionFactory; 

    static NhibernateSessionPerRequest() 
    { 
     _sessionFactory = CreateSessionFactory(); 
    } 

    //all others IHttpModule event and methods are here 
    private static ISessionFactory CreateSessionFactory() 
    { 

     FluentConfiguration configuration = Fluently.Configure().Database(MsSqlConfiguration.MsSql2005. 
                       ConnectionString(x => x.FromConnectionStringWithKey("localdb"))) 
      .Mappings(m => { 
          m.FluentMappings.AddFromAssemblyOf<myproject.model.Request>(); 
          m.FluentMappings.AddFromAssemblyOf<myproject.model.Route>(); 
          } 
        ).ExposeConfiguration((c)=> savedConfig = c);; 

     return configuration.BuildSessionFactory(); 
    } 

    } 

    private static Configuration savedConfig; 

    public static void BuildSchema(NHibernate.Cfg.Configuration config) 
    { 
     new SchemaExport(config).Create(false, true); 
    } 

    public static void BuildSchema(ISession session) 
    { 
     var export = new SchemaExport(savedConfig); 
     export.Execute(false,true,false,session.Connection,null); 
    } 


} 

我为了测试我添加了一个测试项目(类库)表的生成添加模块webconfig

<add name="NhibernateSessionPerRequest" type="myproject.Boostrap.NhibernateSessionPerRequest"/> 

添加参考nunit.framework 2.8.5和myproject。

namespace myproject.Tests 
{ 
    [TestFixture] 
    public class CanGenerateSchemaTestSuite 
    { 
    [Test] 
    public void CanGenarateSchema() 
    { 
     NhibernateSessionPerRequest.BuildSchema(NhibernateSessionPerRequest.GetCurrentSession()); 

    } 
    } 
} 

的测试方法总是失败,我具有此异常:

CanGenerateSchemaTestSuite(1个试验),1次测试失败:儿童测试失败 CanGenarateSchema,失败:System.TypeInitializationException

那么在asp.net环境下如何测试? 感谢您阅读本文。谢谢

回答

5

这是我的一个例子。您需要使用ExposeConfiguration,并通过它接受一个配置的方法,你只要建立数据库那里,然后用SchemaExport

class SqliteRefSessionFactoryProvider : ISessionFactoryProvider 
{ 

    public const string SqliteRefFileName = "ref.db"; 

    public ISessionFactory GetSessionFactory() 
    { 
     return Fluently.Configure().Database(
      SQLiteConfiguration.Standard.UsingFile(SqliteRefFileName).ShowSql()) 
      .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SsoToken>()) 
      .ExposeConfiguration(BuildSchema) 
      .BuildSessionFactory(); 
    } 

    private static void BuildSchema(NHibernate.Cfg.Configuration configuration) 
    { 
     if (File.Exists(SqliteRefFileName)) 
      File.Delete(SqliteRefFileName); 

     new SchemaExport(configuration) 
      .Create(false, true); 
    } 
} 
+0

哦,那真的很有你。我已经看到了这个窍门。但还有一个问题仍然存在。对于if.I '必须在我的'NhibernateSessionPerRequest'类和'BuildSchem'类中创建private.So如何运行'BuildSchema'方法,甚至进一步如何在我的'测试项目'中运行它?谢谢 – 2010-11-17 12:34:19

+0

正如你所看到的,我有自己的ISessionFactoryProvider,它被注入会话工厂单例中。对于单元测试,我使用一个测试来完成构建。这是你的意思吗? – Aliostad 2010-11-17 13:04:31

+0

你好,我没有使用任何类型的依赖注入图书馆,我的问题在这里不能够弄清楚如何在一个单独的项目(意思是我的测试项目)测试这个,我已经更新了这篇文章的标题,并添加新的方法,我从你的答案中得到。请让我知道如果我错了。感谢您的时间 – 2010-11-17 14:03:14

7

刚上其他解决一个模糊的评论;您不需要完全删除数据库文件;只需删除表格:

.ExposeConfiguration(SetupTestDatabase) 

... 

private static void SetupTestDatabase(NHibernate.Cfg.Configuration config) 
{ 
    var schema = new SchemaExport(config); 
    schema.Drop(true, true); 
    schema.Create(true, true); 
} 

这只意味着您可以在不更改其他任何内容的情况下在不同的数据库上运行测试。

编辑; woops;认为这是一个公认的解决方案。如果你正在做一个测试,就这样做吧:

[Test] 
    public void Test_can_store_and_get_objects() 
    { 
     var factory = CreateSessionFactory(); 
     using (var s = factory.OpenSession()) 
     { 
      ... 
     } 
    } 

    private static ISessionFactory CreateSessionFactory() 
    { 
     return Fluently.Configure().Database(SQLiteConfiguration.Standard.UsingFile("firstProject.db")) 
     .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Address>()) // <-- Refer to parent project 
     .ExposeConfiguration(SetupTestDatabase) 
     .BuildSessionFactory(); 
    }