question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tests fail in Visual Studio 16.2 with could not load file or assembly

See original GitHub issue

Since updating to Visual Studio 2019 16.2 my tests keep failing with this error:

Message: 
    Test method Package.Tests.UnitTest1.TestMethod1 threw exception: 
    FakeItEasy.Core.FakeCreationException: 
      Failed to create fake of type xxxxxx:
        No usable default constructor was found on the type xxxxx.
        An exception of type System.IO.FileNotFoundException was caught during this call. Its message was:
        Could not load file or assembly 'xxxx, Version=x.x.x.x, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
           at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
           at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
           at System.Reflection.Emit.TypeBuilder.CreateTypeInfo()
           at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.CreateType(TypeBuilder type) in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\CallConstraintDescriber.cs:line 0
           at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType() in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\CallConstraintDescriber.cs:line 0
           at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateType(String name, Type[] interfaces, INamingScope namingScope) in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\CallConstraintDescriber.cs:line 0
           at Castle.DynamicProxy.Generators.ClassProxyGenerator.<>c__DisplayClass1_0.<GenerateCode>b__0(String n, INamingScope s) in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\CallConstraintDescriber.cs:line 0
           at Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func`3 factory) in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\CallConstraintDescriber.cs:line 0
           at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type[] interfaces, ProxyGenerationOptions options) in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\CallConstraintDescriber.cs:line 0
           at Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options) in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\CallConstraintDescriber.cs:line 0
           at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors) in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\CallConstraintDescriber.cs:line 0
           at FakeItEasy.Creation.CastleDynamicProxy.CastleDynamicProxyGenerator.GenerateInterfaceProxy(Type typeOfProxy, ReadOnlyCollection`1 additionalInterfacesToImplement, IEnumerable`1 attributes, IFakeCallProcessorProvider fakeCallProcessorProvider) in C:\projects\fakeiteasy\src\FakeItEasy\Creation\CastleDynamicProxy\CastleDynamicProxyGenerator.cs:line 41
    

I have a test project (targeting .net472 and using MsTest) that references a nuget package (let’s call it A) that in turn needs another nuget package (B). I want to create a fake for an interface defined in A. This interface has a property of which the type is an interface defined in B.

In Visual Studio 16.2 this now fails if the version of package B is different than the one listed by package with the error message above.

Now there comes the strange thing:

  • This only happens if i run the tests using the Test Explorer. If i run the tests using vstest.console, the tests are successful.
  • Using Visual Studio 16.1, everything works
  • Visual Studio 2017 works
  • Running the tests using resharper works
  • In 16.3 preview, it’s broken with the same error.

I’ve been able to reproduce this on different machines using a small project I created with just the things I need.

Reproduce steps on Visual Studio 16.2:

  • Create 2 class libraries (X, Y) targeting .net standard and net461
  • Define an interface IVeryImportantInterface in X
  • Define an interface IMainInterface in Y
    • Add a property of IVeryImportantInterface to the definition (it now has a dependency on X)
  • Create nuget packages of both projects and use the same version (2.0.0)
  • Create another nugetpackage of the X project and increase the version (3.0.0)
  • Create a test project using mstest and .net472
    • Install FakeItEasy
    • Install package Y using packages.config
    • Add this code
        var test = A.Fake<IMainInterface>();
    
  • The test should work
  • Update the X package to the later version
  • The test should now fail

I been trying to wrap my around it why it fails…

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tomeeraertscommented, Jul 30, 2019

My tests are now working again.

I created a more generic version.

        static UnitTest1()
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                 var requestedAssembly = new AssemblyName(args.Name);
                 var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
                 return loadedAssemblies.SingleOrDefault(x =>  x.GetName().Name.Equals(requestedAssembly.Name, StringComparison.OrdinalIgnoreCase));
            };
        }

I’ve put it here in cause anyone has the same problem.

1reaction
tomeeraertscommented, Jul 29, 2019

I’ve create a repo I also included the packages folder, that way you don’t need to create the packages (they were created using dotnet pack).

There are two branches. One using the same versions where the test passes and one using different version where the test fails

Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not load file or assembly Microsoft.VisualStudio. ...
I recently installed VS2019 Prof 16.2 and experience following error when loading .coverage files: Microsoft Visual Studio Exception was thrown: ...
Read more >
VS 16.2 does not run any tests - Developer Community
Since VS 16.2.x, it is unable to locate the assembly, and test run is interruped with the error in Tests Output. In our...
Read more >
How to fix "Could not load file or assembly" when opening ...
When I open Visual Studio 2022, I receive the error: Could not load file or assembly 'Microsoft.VisualStudio.Diagnostics.
Read more >
Could not load file or assembly 'Microsoft.VisualStudio. ...
It works in production, but when running unit tests on 16.0.2 or 16.1.0 it is failing when calling GetTypes on 'Microsoft.VisualStudio.
Read more >
Why might the 'Could not load file or assembly X' error ...
Review The "Could not load file or assembly 'X'" error message appears and try all the suggestions from it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found