Bug 23387 - TypeInitializationException thrown running unit test
Summary: TypeInitializationException thrown running unit test
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2014-09-25 21:56 UTC by tron.thomas
Modified: 2014-09-30 08:24 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Project contain code that causes the problem (3.73 MB, application/zip)
2014-09-25 21:56 UTC, tron.thomas

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description tron.thomas 2014-09-25 21:56:13 UTC
Created attachment 8195 [details]
Project contain code that causes the problem

Attached is a project for a library that contains unit test using the NUnit testing framework that was built and run on a system running Mac OS X 10.9.5. When the unit tests run a System.TypeInitializationException is thrown causing tests to fail. An equivalent project was created in Visual Studio 2013 on Windows 7 x64, using the exact same code, and all the tests were able to run successfully.

The problem was introduce with the static constructor for the InstanceFactory class was added. Removing the constructor allows the tests to succeed, however the constructor is vital to make sure the class operates correctly.
Comment 1 Mike Krüger 2014-09-26 00:18:26 UTC
Looks like a problem in the runtime.
I get a ReflectionTyipeLoadException trying to run the test.
Comment 2 Marek Safar 2014-09-26 04:56:28 UTC
The error comes from /Applications/Xamarin Studio.app/Contents/Resources/lib/monodevelop/AddIns/NUnit/MonoDevelop.NUnit.dll assembly.

But it's hard to tell what fails to load even if the exception hold tons of Could not load file or assembly 'MonoDevelop.Ide, Version=, Culture=neutral, PublicKeyToken=null' or one of its dependencies. which is suspicious as this assembly should be already loaded
Comment 3 Zoltan Varga 2014-09-26 18:52:11 UTC
You can try running with MONO_LOG_LEVEL=debug to get more info.
Comment 4 tron.thomas 2014-09-27 14:51:59 UTC
@Zoltan. I'm not sure if your comment was meant for me, Marek, or Mike.

I'm also not sure how someone is supposed to apply the MONO_LOG_LEVEL and set to debug. I went into my General section of the Run options and applied MONO_LOG_LEVEL as an environment variable. I ran the unit test. I didn't see any additional output that looked like it would be useful.

I don't know if the stack trace contained things it would have before using the environment variable. Since I can't copy and paste it anyway it would be difficult for me to include in this bug report.
Comment 5 Marek Safar 2014-09-30 08:24:49 UTC
I looked more into this and this behaviour is by design. Your static constructors loads type from all domain assemblies no matter what they are. In XS case there are assemblies which have not loaded dependencies, specifically MonoDevelop.NUnit.dll which has MonoDevelop.Core dependency.

You cannot simply assume that XS and VS test runners environment will give you same set of loaded assemblies. You need to check only for assemblies relevant to your code and ignore rest.