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

NullReferenceException with TestCaseSource if a property has no setter #2052

Closed
provegard opened this issue Feb 23, 2017 · 5 comments · Fixed by #2059
Closed

NullReferenceException with TestCaseSource if a property has no setter #2052

provegard opened this issue Feb 23, 2017 · 5 comments · Fixed by #2059

Comments

@provegard
Copy link

provegard commented Feb 23, 2017

I've written a base class for running NSpec examples using NUnit. I just upgraded it to use NUnit 3, which worked fine. But when I try to target netstandard 1.6, I get the following when running tests:

System.NullReferenceException : Object reference not set to an instance of an object.
   at NUnit.Compatibility.TypeExtensions.NotPrivate(MemberInfo info)
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at NUnit.Compatibility.TypeExtensions.GetAllMembers(Type type)
   at NUnit.Compatibility.TypeExtensions.GetAllMembers(Type type)
   at NUnit.Compatibility.TypeExtensions.GetMember(Type type, String name, BindingFlags ignored)
   at NUnit.Framework.TestCaseSourceAttribute.GetTestCaseSource(IMethodInfo method)
   at NUnit.Framework.TestCaseSourceAttribute.GetTestCasesFor(IMethodInfo method)

The TestCaseSource attribute lives in a class that inherits the nspec class from NSpec, and it has properties without getters, e.g.:

    public virtual Expression<Action> specify
    {
      set
      {
        this.AddExample((ExampleBase) new Example(value, false));
      }
    }

I'm guessing that the exception happens here:

return pinfo.GetMethod.IsPrivate == false;

That code is only used with netstandard 1.6, which explains why things worked well before that.

@provegard
Copy link
Author

Minimal repro

Include the following file in a .NET Core class library targeting .NETStandard 1.6:

    public abstract class TestBase
    {
        public string DontGetMe
        {
            set { }
        }

        public static IEnumerable Provider()
        {
            yield return "test";
        }

        [TestCaseSource(nameof(Provider))]
        public void Test(string data)
        {
            Assert.That(data, Is.Not.Empty);
        }
    }

Then add the following in a .NET Core console application:

    public class TheTest : TestBase
    {
    }

@CharliePoole
Copy link
Contributor

Confirmed by inspection. The cited line needs a ?.

@gcichosz
Copy link
Contributor

Can I grab this?

@CharliePoole
Copy link
Contributor

All yours 😄

@provegard
Copy link
Author

Thanks for the fix! Will there be a hotfix release including this?

@rprouse rprouse modified the milestone: 3.7 Mar 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants