Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A non-collectible assembly may not reference a collectible assembly

Tags:

c#

.net-core

moq

I was trying to migrate .Net Core console app and tests from 2.1 to 3.1 and got a problem in my tests. I was using Moq library and it was working fine. After migration i started to get A non-collectible assembly may not reference a collectible assembly when trying to access any mocked objects. I have reproduced same behavior in completely new project. I was not able to find any relevant information on this topic. I have tested it with both MSTest and Xunit. Is this a Moq library issue or .Net Core 3.1 should use different approach to such cases?

using Moq;
using Xunit;

namespace NetCore3.Tests
{
    public interface IMyInterface{ }
    
    public class UnitTest
    {
        [Fact]
        public void Test()
        {
            var mock = new Mock<IMyInterface>();
            var tmp = mock.Object; // this line throwing exception
        }
    }
}

Exception:

NetCore3.Tests.UnitTest.Test

System.NotSupportedException: A non-collectible assembly may not reference a collectible assembly.

System.NotSupportedException
A non-collectible assembly may not reference a collectible assembly.
   at System.Reflection.Emit.ModuleBuilder.GetTypeRef(QCallModule module, String strFullName, QCallModule refedModule, String strRefedModuleFileName, Int32 tkResolution)
   at System.Reflection.Emit.ModuleBuilder.GetTypeRefNested(Type type, Module refedModule, String strRefedModuleFileName)
   at System.Reflection.Emit.ModuleBuilder.GetTypeTokenWorkerNoLock(Type type, Boolean getGenericDefinition)
   at System.Reflection.Emit.ModuleBuilder.GetTypeTokenInternal(Type type, Boolean getGenericDefinition)
   at System.Reflection.Emit.TypeBuilder.AddInterfaceImplementation(Type interfaceType)
   at Castle.DynamicProxy.Generators.Emitters.ClassEmitter..ctor(ModuleScope modulescope, String name, Type baseType, IEnumerable`1 interfaces, TypeAttributes flags, Boolean forceUnsigned)
   at Castle.DynamicProxy.Generators.BaseProxyGenerator.BuildClassEmitter(String typeName, Type parentType, IEnumerable`1 interfaces)
   at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.Init(String typeName, ClassEmitter& emitter, Type proxyTargetType, FieldReference& interceptorsField, IEnumerable`1 interfaces)
   at Castle.DynamicProxy.Generators.InterfaceProxyWithoutTargetGenerator.GenerateType(String typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope)
   at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.<>c__DisplayClass6_0.<GenerateCode>b__0(String n, INamingScope s)
   at Castle.Core.Internal.SynchronizedDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func`3 factory)
   at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)
   at Moq.Mock`1.InitializeInstance()
   at Moq.Mock`1.OnGetObject()
   at Moq.Mock`1.get_Object()
   at NetCore3.Tests.UnitTest.Test() in C:\Work\NetCore3\NetCore3.Tests\UnitTest1.cs:line 14

Project file:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <IsPackable>false</IsPackable>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
        <PackageReference Include="Moq" Version="4.14.5" />
        <PackageReference Include="xunit" Version="2.4.1" />
        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.2"/>
        <PackageReference Include="coverlet.collector" Version="1.3.0"/>
    </ItemGroup>
</Project>
like image 707
Artyom Avatar asked Jul 24 '20 15:07

Artyom


1 Answers

For the sake of anyone else landing on this from google...

We are using the latest Rider/ReSharper EAP (2020.2 EAP 8) which has introduced this bug when:

  • Running tests via the Rider/ReSharper test runner
  • The test project references and uses a mocking library that attempts to generate dynamic proxies (Moq, NSubstitute, etc...)

This has been fixed by JetBrains in their development branch and will be fixed in the next EAP. You can track this here: https://youtrack.jetbrains.com/issue/RIDER-48134

Update: EAP 9 is out which fixes this 🎉 You can use JetBrains Toolbox to update.

like image 197
Stuart Avatar answered Nov 01 '22 15:11

Stuart