Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test in Unity - Getting an InvalidCastException - Why? And how to fix?

I try to be as specific and helpful as possible in my questions here on S.O. but for this one, well, that means almost nothing, as I have absolutely no hint of the faintest clue about why this is happening.

It happens on any/every test (even "blank" ones). And I already tried (uninstalling and) updating unity. - Didn't work.

Anyways, here is the stacktrace of the exception:

2017.11.16 14:54:24.308   ERROR System.Reflection.TargetInvocationException: Uma exceção foi acionada pelo destino de uma chamada.
System.Reflection.TargetInvocationException: Uma exceção foi acionada pelo destino de uma chamada. ---> System.InvalidCastException: Não é possível converter um objeto do tipo 'NUnit.Engine.CallbackHandler' no tipo 'System.Web.UI.ICallbackEventHandler'.
   em NUnit.Framework.Api.FrameworkController.LoadTestsAction..ctor(FrameworkController controller, Object handler)
   --- Fim do rastreamento de pilha de exceções internas ---
   em System.RuntimeMethodHandle._InvokeConstructor(Object[] args, SignatureStruct& signature, IntPtr declaringType)
   em System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   em System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   em System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
   em System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
   em System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
   em System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
   em NUnit.Engine.Drivers.NUnit3FrameworkDriver.Load(String testAssemblyPath, IDictionary`2 settings)
   em NUnit.Engine.Runners.DirectTestRunner.LoadPackage()
   em NUnit.Engine.Runners.AbstractTestRunner.Load()
   em NUnit.Engine.Runners.MasterTestRunner.LoadPackage()
   em NUnit.Engine.Runners.AbstractTestRunner.EnsurePackageIsLoaded()
   em NUnit.Engine.Runners.MasterTestRunner.NUnit.Engine.ITestRunner.Explore(TestFilter filter)
   em JetBrains.ReSharper.UnitTestRunner.nUnit30.BuiltInNUnitRunner.<>c__DisplayClass1.<RunTests>b__0()
   em JetBrains.ReSharper.UnitTestRunner.nUnit30.BuiltInNUnitRunner.WithExtensiveErrorHandling(IRemoteTaskServer server, Action action)

This is making me pretty much code without (unit-)testing for the last couple of days, so if anyone has any idea about the cause and/or solutions, that would be greatly appreciated.

like image 204
XenoRo Avatar asked Oct 29 '22 23:10

XenoRo


1 Answers

Resharper is using the NUnit Engine to run tests But the engine only supports tests written against .NET 2.0 through 4.5. The framework you are using is one of the .NET Standard builds. I can tell this by looking at the stack trace and seeing that it doesn't have a reference to System.Web.UI.ICallbackEventHandler.

You should be able to run your tests in Test Explorer using the latest installation of the NUnit 3 VS Test Adapter.

Updating for clarity...

There's no doubt that are using a version of the nunit framework that is not supported by the platform. That's the key here.

Some builds of the framework use System.Web.UI.ICallbackEventHandler(.NET 2.0, 3.5, 4.0 and 4.5) while others (.NET Standard 1.3 and 1.6) do not. Since NUnit is looking for that interface, you are referencing one of the builds that uses it. Since it's not available, you are on a platform that doesn't include it.

A simple workaround would be for you to define the interface yourself, duplicating it exactly as defined by Microsoft. You don't have to implement it, since the framework you are using does that. Of course, that could lead to other failures due to framework / platform incompatibility so it's probably better to resolve the compatibility issue in the first place.

Unfortunately, I can't be of any help on the Unity side of the problem.

like image 168
Charlie Avatar answered Nov 15 '22 12:11

Charlie