NullReferenceExpection在一个asp.net nhibernate解决方案的集成测试项目

问题描述:

我有一个.NET 3.5解决方案与一个asp.net(网站)项目与fluentnhibernate和它的测试项目(类库项目)。我引用了测试项目中的asp.net项目以及所有fluentnhibernate/nhibenate dll。NullReferenceExpection在一个asp.net nhibernate解决方案的集成测试项目

我不明白的是,在运行webform(从浏览器命中)时,让我们说Test.aspx,架构的构建是成功的,我可以看到我的数据库中的表。 这里是我的Test.aspx.cs

致电
public partial class Test : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     ISession session = SessionManager.Instance.OpenSession(); 
     SessionManager.BuildSchema(session); 
    } 
} 

我碰巧使用在我的测试类的CanGenerateSchema方法相同的方法方法,它总是失败

[TestFixture] 
public class CanGenerateSchemaTestSuite 
{ 
    [Test] 
    public void CanGenarateSchema() 
    { 
     ISession session = SessionManager.Instance.OpenSession(); 
     SessionManager.BuildSchema(session); 
    } 
} 

这里是SessionManager我使用:

public sealed class SessionManager 
{ 
    private readonly ISessionFactory _sessionFactory; 


    public static ISessionFactory SessionFactory 
    { 
     get { return Instance._sessionFactory; } 
    } 


    private ISessionFactory GetSessionFactory() 
    { 
     return _sessionFactory; 
    } 

    public static SessionManager Instance 
    { 
     get { return NestedSessionManager._sessionManager; } 
    } 

    public ISession OpenSession() 
    { 
     return Instance.GetSessionFactory().OpenSession(); 
    } 


    private static Configuration SaveConfigs; 

    private SessionManager() 
    { 
     try 
     { 

      if (_sessionFactory == null) 
      { 
      //from the debugging the code breaks from here when trying to get connectionstring. 
       string constring = ConfigurationManager.AppSettings["localdb"].ToString(); 
       FluentConfiguration configuration = Fluently.Configure() 
        .Database(
         MsSqlConfiguration.MsSql2005.ConnectionString(constring)) 
        .Mappings(m => 
        { 
         m.FluentMappings.AddFromAssemblyOf<myproject.model.Request>(); 
         m.FluentMappings.AddFromAssemblyOf<myproject.model.Route>(); 
        }) 
        .ExposeConfiguration((x) => 
        { 
         SaveConfigs = x; 
         x.SetProperty("current_session_context_class", "thread_static"); 

        }); 
       _sessionFactory = configuration.BuildSessionFactory(); 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.Write(ex.Message); 

     } 

    } 


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

    class NestedSessionManager 
    { 
     internal static readonly SessionManager _sessionManager = new SessionManager(); 
    } 
} 

所以从我的评论NullReferenceException发生在访问connectionstring时。我没有解释为什么会发生这种情况。我肯定这是一些棘手的问题,我无法克服它。如果有人能在这里给我一只手,我将非常感激。谢谢你的阅读。

ConfigurationManager.AppSettings["localdb"] from Test.aspx将从您的web.config文件在Web项目中拉动。

该文件将无法访问您的测试项目(我假设您的测试是从您的网站单独的项目)。你应该能够通过使用正确的localdb设置将app.config文件添加到测试项目中,或者使用FluentNHibernate的流畅构建器而不是使用配置字符串来解决此问题。

举例app.config文件:

<?xml version="1.0"?> 
<configuration> 
<appSettings> 
    <add key="localdb" value="yourconnectionstring" /> 
</appSettings> 
</configuration> 
+0

thanks.i想过这一点,在我看来,这将是多余的具有测试project.It另一ConnectionString的工作现在。但这是一种好的做法吗?只是一个问题 – 2010-11-19 19:05:48

+0

这取决于它变化的频率。如果你还没有,也许会想到Fluent Builders,那么你的代码就被编译在一个地方。 http://wiki.fluentnhibernate.org/Database_configuration – 2010-11-19 19:09:57

如果ConfigurationManager.AppSettings["localdb"]返回的值是null那么.ToString()调用将导致NullReferenceException

确保存在“localdb”设置。