Having a problem with NCrunch where the majority of my tests are failing due to this error:
Assembly initialize/cleanup failed: System.Diagnostics.Contracts.__ContractsRuntime+ContractException: Assertion failed: Dependency.instance == null Cannot set dependency resolver once it has been set.
The code is being called from within the [AssemblyInitialize] method of my test project to set up a StructureMap dependency resolver. We use CodeContracts to ensure the dependency resolver is only set once (it's a Singleton/static instance):
public static void SetResolver(IDependencyResolver resolver)
{
Contract.Assert(Dependency.instance == null, "Cannot set dependency resolver once it has been set");
instance = resolver;
}
I am guessing the failure is to do with some kind of parallelization but have tried disabling parallel test execution, setting NCrunch CPU cores to 1, max number of processing threads to 1 but the problem still happens. If I click on a failed test and run it individually, it works so I know NCrunch is capable of running the test successfully, just not when all tests are being run even if they're supposed to run sequentially!
Any advice? I really want to use NCrunch but this is a big solution I'm working on for a large company and I can't change the structuremap settings (besides which the tests run fine through MSTest so technically there's nothing wrong with the project config).
Edit: it may be worth mentioning that my solution has 17 projects, 4 of which are test projects and each one will have a similar [AssemblyInitialize] routine to set up the structuremap.