Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue when running a test project targetting a Web API. #184

Closed
AbhitejJohn opened this issue May 16, 2017 · 7 comments
Closed

Issue when running a test project targetting a Web API. #184

AbhitejJohn opened this issue May 16, 2017 · 7 comments

Comments

@AbhitejJohn
Copy link
Contributor

From @mailbala

The WebAPi project targets net462 and Output type is exe . In the unit test project, I am using NSubstitute to mock the dependencies. When I run the test, I am getting the below error

Message: Initialization method WebApi.UnitTest.Controllers.HomeControllerV1Test.TestInitializer threw exception. System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).

When I use Moq, getting the below error.

Message: Test method WebApi.UnitTest.Controllers.HomeControllerV1Test.TestUserList threw exception:
System.BadImageFormatException: Could not load file or assembly 'CustomerInsights.WebApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

@AbhitejJohn
Copy link
Contributor Author

@mailbala: Can you share with us a repro project please?

@skynetit
Copy link

skynetit commented May 16, 2017

@AbhitejJohn I figured out the issue. I have a class library dependency for the webapi proj which was targeting netstandard1.5. When I changed to netstandard1.4, tests are running successfully.

Thanks!

@AbhitejJohn
Copy link
Contributor Author

@mailbala : That seems more of a workaround to me. From here net461/net462 should be compatible with netstandard1.5. What is your unit test project targeting?

@skynetit
Copy link

@AbhitejJohn unit test project is targeting net462.
I am using SDK 1.0.4. netstandard1.4 works perfectly.

Message: Initialization method
WebApi.UnitTest.Controllers.HomeControllerV1Test.TestInitializer threw exception. System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).

StackTrace:
at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
at System.Reflection.RuntimeMethodInfo.GetParameters()
at Castle.DynamicProxy.Generators.MethodSignatureComparer.GetHashCode(MethodInfo obj)
at System.Collections.Generic.Dictionary2.FindEntry(TKey key) at Castle.DynamicProxy.Generators.MethodFinder.RemoveDuplicates(MethodInfo[] infos) at Castle.DynamicProxy.Generators.MethodFinder.GetAllInstanceMethods(Type type, BindingFlags flags) at Castle.DynamicProxy.Contributors.MembersCollector.CollectMethods(IProxyGenerationHook hook) at Castle.DynamicProxy.Contributors.MembersCollector.CollectMembersToProxy(IProxyGenerationHook hook) at Castle.DynamicProxy.Contributors.InterfaceProxyWithoutTargetContributor.<CollectElementsToProxyInternal>d__0.MoveNext() at Castle.DynamicProxy.Contributors.CompositeTypeContributor.CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) at Castle.DynamicProxy.Generators.InterfaceProxyWithoutTargetGenerator.GenerateType(String typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope) at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.<>c__DisplayClass1.<GenerateCode>b__0(String n, INamingScope s) at Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func3 factory)
at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)
at NSubstitute.Proxies.CastleDynamicProxy.CastleDynamicProxyFactory.CreateProxyUsingCastleProxyGenerator(Type typeToProxy, Type[] additionalInterfaces, Object[] constructorArguments, IInterceptor interceptor, ProxyGenerationOptions proxyGenerationOptions)
at NSubstitute.Proxies.CastleDynamicProxy.CastleDynamicProxyFactory.GenerateProxy(ICallRouter callRouter, Type typeToProxy, Type[] additionalInterfaces, Object[] constructorArguments)
at NSubstitute.Proxies.ProxyFactory.GenerateProxy(ICallRouter callRouter, Type typeToProxy, Type[] additionalInterfaces, Object[] constructorArguments)
at NSubstitute.Core.SubstituteFactory.Create(Type[] typesToProxy, Object[] constructorArguments, SubstituteConfig config)
at NSubstitute.Core.SubstituteFactory.Create(Type[] typesToProxy, Object[] constructorArguments)
at NSubstitute.Substitute.For(Type[] typesToProxy, Object[] constructorArguments)
at NSubstitute.Substitute.For[T](Object[] constructorArguments)
at WebApi.UnitTest.Controllers.HomeControllerV1Test.TestInitializer()

@AbhitejJohn
Copy link
Contributor Author

Saw an instance of a similar issue here: dotnet/standard#295 . Does that help? Seems more of a .Net dependency resolution issue, so you might want to get help from the experts in the dotnet repo as well.

@skynetit
Copy link

@AbhitejJohn I tried to refer NETStandard.Library library in net462 unit test project. But faced the same issue

@AbhitejJohn
Copy link
Contributor Author

AbhitejJohn commented May 22, 2017

@mailbala : I took a deeper look at this and this was pretty straight forward to repro. This has been fixed here: microsoft/vstest#428 and should work with the latest pre-release version of Microsoft.Net.Test.Sdk.
To give you a brief summary, there should have been a binding redirect generated for System.Runtime from 4.1.0.0 (referred by the ns1.5 Class Library) to 4.1.1.0 (which the ASP.NET Core app refers to in net462). The fix for the above does just this in an auto-generated config file for the test project.

Do feel free to re-open this issue if the above does not work for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants