ssannandeji / Zenject-2019

Dependency Injection Framework for Unity3D
MIT License
2.53k stars 363 forks source link

Null reference exeption if name of test is different #537

Closed Xonoroff closed 5 years ago

Xonoroff commented 6 years ago

Hi! I'm using NUnit framework that is provided by Unity to create tests.

When i'm using working with [Test] and [TestCaseSource("CalcTestCases")] that reffers to method, that returns diffrenet test cases with custom name - I got an error. at Zenject.ZenjectIntegrationTestFixture.CurrentTestHasAttribute[ValidateOnlyAttribute] () [0x0005f] in \ZenjectIntegrationTestFixture\ZenjectIntegrationTestFixture.cs:104 at Zenject.ZenjectIntegrationTestFixture.PreInstall () [0x00038] in \ZenjectIntegrationTestFixture.cs:71 at Tests.PlayModeTests.CacheRepositoryTests.Setup () [0x00002] in \MyClass.cs:18 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000d0] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222 private static IEnumerable<TestCaseData> CalcTestCases() { yield return new TestCaseData(1,1) { TestName = "Test sum"}; } It's obvous bug, if we look into CurrentTestHasAttribute method in ZenjectIntegrationTestFixture.

Xonoroff commented 6 years ago

PS. I think the best way to fix it - change method to something like this. I don't understand why you have to parse TestContext.CurrentContext.Test.FullName if you have TestContext.CurrentContext.Test.MethodName prop bool CurrentTestHasAttribute<T>() where T : Attribute { return this.GetType().GetMethod(TestContext.CurrentContext.Test.MethodName).GetCustomAttributes(true) .Cast<Attribute>().OfType<T>().Any(); }

svermeulen commented 5 years ago

Thanks - I changed it to your suggestion