SetUp:Spring.Objects.Factory.ObjectDefinitionStoreException:使用名称' NHibernateSessionFactory'注册对象时出错。在'程序集中定义

时间:2015-06-18 17:54:24

标签: nhibernate nunit nhibernate-mapping spring.net

当我进行单元测试时,这就是我得到的错误:

SetUp : Spring.Objects.Factory.ObjectDefinitionStoreException : Error registering object with name 'NHibernateSessionFactory' defined in 'assembly [Eiq.Middleware.Data.DomainRepository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5c61376b85e43767], resource [Eiq.Middleware.Data.DomainRepository.Persistence.xml] line 15' : Could not resolve placeholder 'TDM.providerName'.
   at Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer.ProcessProperties(IConfigurableListableObjectFactory factory, NameValueCollection props)
   at Spring.Objects.Factory.Config.PropertyResourceConfigurer.PostProcessObjectFactory(IConfigurableListableObjectFactory factory)
   at Spring.Context.Support.AbstractApplicationContext.InvokeObjectFactoryPostProcessors(IList objectFactoryPostProcessors, IConfigurableListableObjectFactory objectFactory)
   at Spring.Context.Support.AbstractApplicationContext.InvokeObjectFactoryPostProcessors(IConfigurableListableObjectFactory objectFactory)
   at Spring.Context.Support.AbstractApplicationContext.Refresh()
   at Spring.Context.Support.XmlApplicationContext..ctor(XmlApplicationContextArgs args)
   at Spring.Context.Support.XmlApplicationContext..ctor(String[] configurationLocations)
   at Spring.Testing.NUnit.AbstractSpringContextTests.LoadContextLocations(String[] locations)
   at Spring.Testing.NUnit.AbstractDependencyInjectionSpringContextTests.LoadContextLocations(String[] locations)
   at Spring.Testing.NUnit.AbstractSpringContextTests.GetContext(Object key)
   at Spring.Testing.NUnit.AbstractDependencyInjectionSpringContextTests.SetUp()

有什么方法可以让它以某种方式传递" TDM.providerName"的值。通过在我的单元测试app.config中设置一些东西?这是我尝试过的,但它没有帮助,测试项目的名称是MiddlewareTests,默认名称空间是Eiq.Middleware.SmokeTest:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="databaseProperties" type="System.Configuration.NameValueSectionHandler, System" />
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
      <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" restartOnExternalChanges="true" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="DefaultUserName" value="tdmuser" />
    <!-- Well Query Limit -->
    <add key="WellQueryLimit" value="50000" />
    <add key="TDM.providerName" value="SqlServer-2.0"/>
  </appSettings>
  <spring>
    <parsers>
      <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
      <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" />
      <parser type="Spring.Aop.Config.AopNamespaceParser, Spring.Aop" />
    </parsers>
    <context>
      <resource uri="assembly://MiddlewareTests/Eiq.Middleware.SmokeTest/Persistence.xml" />
      <resource uri="assembly://MiddlewareTests/TEiq.Middleware.SmokeTest/Repositories.xml" />
      <resource uri="assembly://MiddlewareTests/Eiq.Middleware.SmokeTest/Services.xml" />
      <resource uri="config://spring/objects" />
    </context>
    <objects configSource="config\spring.config" />
  </spring>
</configuration>

或者,或者,我怎样才能让应用程序指向我已经修改过的Persistence.xml,Repositories.xml和Services.xml文件的副本,以便直接在我的单元测试项目中进行测试?有没有人有任何其他建议?

1 个答案:

答案 0 :(得分:1)

要解决上述问题,请执行几个步骤,但我不确定具体修复了哪个步骤:

  1. 在我的测试项目和NHibernate.Caches.SysCache,版本3.1.0.4000
  2. 中添加了对NHibernate.Validator,Version = 1.3.1.4000的引用
  3. 在我的测试项目中添加了参考log4net,Version = 1.2.10.0以打印出更多诊断信息
  4. 在我的测试项目中添加了对System.Configuration,System.Web.ApplicationServices和System.Web.Extensions的引用
  5. 从我的测试项目中删除了对Spring.Core和Spring.Data的引用。
  6. 此外,将我的测试项目中的App.config文件更新为以下内容,其中关键部分是与hibernate-configuration相关的部分:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="databaseProperties" type="System.Configuration.NameValueSectionHandler, System" />
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
        <sectionGroup name="spring">
          <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
          <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
          <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" restartOnExternalChanges="true" />
        </sectionGroup>
        <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
        <section name="nhv-configuration" type="NHibernate.Validator.Cfg.ConfigurationSectionHandler, NHibernate.Validator" requirePermission="false" />
        <section name="auditConfig" type="Eiq.Middleware.Common.Config.AuditConfigSectionHandler, Eiq.Middleware.Common" restartOnExternalChanges="true" />
        <section name="coordSys" type="Eiq.Middleware.Common.Config.CoordSysSectionHandler, Eiq.Middleware.Common" restartOnExternalChanges="true" />
      </configSections>
      <log4net>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message%newline" />
          </layout>
        </appender>
        <root>
          <level value="INFO" />
          <appender-ref ref="ConsoleAppender" />
        </root>
      </log4net>
      <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
          <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
          <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
          <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
          <property name="connection.connection_string">Data Source=localhost;Initial Catalog=ENERGYIQ;User Id=svc_tdm;Password=svc_tdm;    
      </property>
      <mapping assembly="TdmTests" />
    </session-factory>
      <nhv-configuration xmlns="urn:nhv-configuration-1.0">
        <shared_engine_provider class="NHibernate.Validator.Event.NHibernateSharedEngineProvider, NHibernate.Validator" />
      </nhv-configuration>
      <auditConfig configSource="Config\audit.config" />
      <connectionStrings configSource="config\connectionStrings.config" />
      <spring>
        <parsers>
          <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
          <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" />
          <parser type="Spring.Aop.Config.AopNamespaceParser, Spring.Aop" />
        </parsers>
        <context>
          <resource uri="assembly://TdmTests/TdmTests/Persistence.xml" />
          <resource uri="assembly://TdmTests/TdmTests/Repositories.xml" />
          <resource uri="assembly://TdmTests/TdmTests/Services.xml" />
          <resource uri="config://spring/objects" />
        </context>
        <objects configSource="config\spring.config" />
      </spring>
      <appSettings>
        <add key="DefaultUserName" value="tdmuser" />
        <!-- Well Query Limit -->
        <add key="WellQueryLimit" value="50000" />
        <add key="ClientSettingsProvider.ServiceUri" value="" />
      </appSettings>
      <system.web>
        <membership defaultProvider="ClientAuthenticationMembershipProvider">
          <providers>
            <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
          </providers>
        </membership>
        <roleManager defaultProvider="ClientRoleProvider" enabled="true">
          <providers>
            <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
          </providers>
        </roleManager>
      </system.web>
    </configuration>
    

    HTH。