Irakli Lekishvili
Irakli Lekishvili

Reputation: 34198

The type initializer for 'NHibernate.Cfg.Configuration' threw an exception. Unit Testing

I have UnitTesting project with one TestMethod which calls repository and repository creates new record in PostgreSQL

When I run this test its throws exception:

Message: The type initializer for 'NHibernate.Cfg.Configuration' threw an exception.

Inner Exception: {"The type initializer for 'NHibernate.LoggerProvider' threw an exception."}

StackTrace:

   at NHibernate.Cfg.Configuration..ctor()
   at FluentNHibernate.Cfg.FluentConfiguration..ctor()
   at FluentNHibernate.Cfg.Fluently.Configure()
   at Woo.Test.UnitTest.FillCompany() 

My repository works good when i call it from ASP.NET MVC Applictaion

This is my Fluent NHibernate configuration:

_configuration = Fluently.Configure().Database(PostgreSQLConfiguration.Standard.ConnectionString(c => c
               .FromConnectionStringWithKey("DefaultConnection"))).Mappings(m => m.FluentMappings.AddFromAssemblyOf<Company>())
               .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)).BuildConfiguration();
_sessionFactory = _configuration.BuildSessionFactory();

So problem is only with unit testing project.

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configurations>
    <connectionStrings>
      <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Server=localhost;Port=5432;Database=Test;User=postgres;Password=Test;" />
    </connectionStrings>
  </configurations>
</configuration>

Upvotes: 0

Views: 3598

Answers (2)

Davita
Davita

Reputation: 9144

Unit test deployment works differently. You should mark any required file/assembly to be deployed within bin directory from *.testsettings file. See the screen below:

enter image description here

All you have to do now is to find the missing file/assembly and deploy it via testsettings file.

Upvotes: 1

Peter
Peter

Reputation: 27944

You app.config has to be in the bin directory of the unit test. Check the properties of the app.config and see if it is deployed.

Upvotes: 1

Related Questions