0

I am trying to get an MSTest unit test running, and I have the following line in my NHibernate config section in App.config:

<property name="proxyfactory.factory_class">NHibernate.ByteCode.DefaultProxyFactoryFactory, NHibernate</property>

Whenever I try and run the unit test, I get an exception in the ClassInitialize method for the test ficture. The exception is:

NHibernate.Bytecode.UnableToLoadProxyFactoryFactoryException: Unable to load type 'NHibernate.ByteCode.DefaultProxyFactoryFactory, NHibernate.Bytecode' during configuration of proxy factory class.

and my initializer code is:

private static ISessionFactory _sessionFactory;
private static Configuration _configuration;

[ClassInitialize()]
public static void ProductRepositoryInitialize(TestContext testContext)
{
    _configuration = new Configuration();
    _configuration.Configure();
    _configuration.AddAssembly(typeof(Product).Assembly);
    _sessionFactory = _configuration.BuildSessionFactory();
}

Yet the DefaultProxyFactoryFactory is available, as I can instantiate in in a console application using the line:

NHibernate.Bytecode.DefaultProxyFactoryFactory factory = new DefaultProxyFactoryFactory();

So why is NHibernate failing to instantiate, or even find, this type in my unit test class. Is it perhaps the test class initializer is static?

BREAKING NEWS: If I use the same proxy factory but with fluent configuration, I get happiness and joy, and rows inserted into my data store.

_factory = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2012)
    .Mappings(m => m.HbmMappings.AddFromAssemblyOf<Product>())
    .ProxyFactoryFactory<DefaultProxyFactoryFactory>()
    .BuildSessionFactory();
ProfK
  • 49,207
  • 121
  • 399
  • 775
  • Are you sure that you have `, NHibernate` in your .config-file? The exception you're getting suggests otherwise. – Anton Gogolev Dec 21 '15 at 10:04
  • @AntonGogolev Yes, that line in my config is exactly as its code excerpt above shows. if you scroll it to the right. – ProfK Dec 21 '15 at 10:33

2 Answers2

1

I may come from issue related to MsTest (Vs2012) See Here

You can also try the DeploymentItemAttribute

If it's still failing with NUnit may you check that all NHibernate dependencies are present in the directory.

Community
  • 1
  • 1
Fab
  • 14,327
  • 5
  • 49
  • 68
  • I installed NHibernate using Nuget. The only files in the package are `NHibernate.dll` and `NHibernate.xml`, and in the output folder after build, there are only these two NHibernate files, plus `Iesi.Collections.dll` and `Iesi.Collections.xml`. The `DefaultProxyFactoryFactory` class is supposed to be internal to NHIbernate and require no further dependencies. I will check your link for MSTest, even though I'm on VS2015, bit thank you for trying to help. – ProfK Dec 24 '15 at 13:45
0

PascalCase mindset plus IDE autocompletion betrays you ;)

Wrong:

<property name="proxyfactory.factory_class">NHibernate.ByteCode.DefaultProxyFactoryFactory, NHibernate</property>

Correct:

<property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate</property>

Why Bytecode doesn't meet the format of the other Namespaces?

Because Bytcode is a full word not a composition of words so Pascal or Camel Case doesn't apply here.

Anyway, I am not sure of this but I think that if you omit factory_class property in config DefaultProxyFactoryFactory is created by default. Could somebody confirm it?

jlvaquero
  • 8,571
  • 1
  • 29
  • 45
  • I have long given up on xml based config in favour of fluent, but I strongly suspect you are correct, based on the code excerpts in my OP. I definitely had `ByteCode` in the XML, and definitely (thanks to autocomplete), had `Bytecode` in the test code where I could instantiate the class. Well spotted. – ProfK Dec 30 '15 at 12:43