2
votes

I've got a set on unit tests, which, if I run all together (using resharper) I get this error:

SetUp : System.TypeInitializationException : The type initializer for 'FakeItEasy.Core.FakeScope' threw an exception. ----> System.IO.FileLoadException : API restriction: The assembly 'file:///C:\Users\abrown\Documents\Repos\ink.services.jetstar\My.Namespace.Tests.Unit\bin\Debug\My.Namespace.dll' has already loaded from a different location. It cannot be loaded from a new location within the same appdomain.

If I run them individually, they pass.

Further down in the exception, it's failing on lines like this:

[SetUp]
public void SetUp()
{
    _myFake = A.Fake<ISomething>();

EDIT

This also happens if I use the 'standard' NUnit Test Runner.

As @david-arno said in the comments, by changing the settings in the ReSharper runner, that fixes it (obviously only for re-sharper)

For completeness, here's a full stack trace

My.CompanyNamespace.ProjectName.Tests.Unit.DownloadAndStoreContentTests.gets_content_downloader_for_each_section_in_index: SetUp : System.TypeInitializationException : The type initializer for 'FakeItEasy.Core.FakeScope' threw an exception. ----> System.IO.FileLoadException : API restriction: The assembly 'file:///C:\Users\abrown\Documents\Repos\My.CompanyNamespace\My.CompanyNamespace.ProjectName.Tests.Unit\bin\Debug\My.CompanyNamespace.ProjectName.dll' has already loaded from a different location. It cannot be loaded from a new location within the same appdomain.

at FakeItEasy.Core.FakeScope.get_Current() at FakeItEasy.IoC.DictionaryContainer.<>c_DisplayClass11.<Register>b__0(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) at FakeItEasy.ServiceLocator.Resolve[T]() at FakeItEasy.RootModule.<RegisterDependencies>b__3(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.<>c__DisplayClass11.b_0(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) at FakeItEasy.ServiceLocator.ResolveT at FakeItEasy.RootModule.b_1a(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.<>c_DisplayClass11.<Register>b__0(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) at FakeItEasy.ServiceLocator.Resolve[T]() at FakeItEasy.RootModule.<RegisterDependencies>b__19(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.<>c__DisplayClass11.b__0(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) at FakeItEasy.ServiceLocator.ResolveT at FakeItEasy.A.FakeT at My.CompanyNamespace.ProjectName.Tests.Unit.DownloadAndStoreContentTests.SetUp() in c:\Users\abrown\Documents\Repos\My.CompanyNamespace\My.CompanyNamespace.ProjectName.Tests.Unit\DownloadAndStoreContentTests.cs:line 23 --FileLoadException at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark) at System.Reflection.Assembly.ReflectionOnlyLoadFrom(String assemblyFile) at FakeItEasy.Core.ApplicationDirectoryAssembliesTypeCatalogue.GetAllAvailableAssemblies() at FakeItEasy.Core.ApplicationDirectoryAssembliesTypeCatalogue..ctor() at FakeItEasy.ImportsModule.b_1(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.SingletonResolver1.UnresolvedState.Resolve(DictionaryContainer container) at FakeItEasy.IoC.DictionaryContainer.<>c__DisplayClass11.b_0(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) at FakeItEasy.ServiceLocator.ResolveT at FakeItEasy.ImportsModule.b_0(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.SingletonResolver1.UnresolvedState.Resolve(DictionaryContainer container) at FakeItEasy.IoC.DictionaryContainer.<>c__DisplayClass11.b_0(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) at FakeItEasy.ServiceLocator.ResolveT at FakeItEasy.ImportsModule.b_4[T](DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.SingletonResolver1.UnresolvedState.Resolve(DictionaryContainer container) at FakeItEasy.IoC.DictionaryContainer.<>c__DisplayClass11.b__0(DictionaryContainer c) at FakeItEasy.IoC.DictionaryContainer.Resolve(Type componentType) at FakeItEasy.ServiceLocator.ResolveT at FakeItEasy.Core.FakeScope.RootScope..ctor() at FakeItEasy.Core.FakeScope..cctor()

1
It sounds like one of your tests is loading the original assembly before the mock is set up.SLaks
Where would it be doing that?Alex
I have no idea. Read through your code.SLaks
... that's a good idea, I hadn't thought of doing that ;-) I meant... project / assembly reference? Or somewhere literally in the code?Alex
Are you using Rsharper 8? If so, check out stackoverflow.com/questions/17877471/…. This may fix the problem. It did for me when I had a similar problem.David Arno

1 Answers

4
votes

I think this is arising because:

  1. on (or near) startup, FakeItEasy scans its working directory looking for DLLs that may include extension points,
  2. you have multiple test assemblies in different directories, at least two of which include copies of the same assembly, and
  3. the test runners are running each test assembly's tests in the same AppDomain

The reason that running the test assemblies in parallel works is not due to the parallelness, but because the test runner will then run the assemblies in separate AppDomains.

Other workarounds that should bring relief would be running the assemblies' tests in separate AppDomains (for example, using NUnit's /domain=Multiple parameter) or even separate processes (/process=Multiple). I expect many test runners support these options. I don't have ReSharper 8, but

We're working on making FakeItEasy more forgiving of failures to load assemblies. I've added more details at FakeItEasy issue 189 - thanks for submitting that.

Update The above-mentioned issue 189 has been fixed in FakeItEasy 1.15.0. Get it or a later version from NuGet!