Paramaterising the arranged property of a test

4 posts, 0 answers
  1. David
    David avatar
    2 posts
    Member since:
    Sep 2017

    Posted 14 Sep 2017 Link to this post

    I have a class which has a large number of properties each of which returns a double, and a method under test which takes a string, and an instance of the class, and calls one of these properties based on the value of the string (effectively, it calls the property whose name is the string). I am using NUnit, which provides for parameterised testing. I would like to test the method with a set strings, but this means arranging for the specific property call. I am quite close, but I can't quite get it to work.

    So far I have the following. A set of test parameters, defined as a Dictionary:

    public static Dictionary<string, System.Linq.Expressions.Expression<Func<IMyClass, double>>> SpecialProperties = new Dictionary<string, System.Linq.Expressions.Expression<Func<IMyClass, double>>>()
    {
        {"InlineLength", x=>x.InLineLength},
        {"BranchLength", x=>x.BranchLength},
        {"TotalLength", x=>x.TotalLength},
        {"CentreLineLength", x=>x.CentreLineLength},
        {"SurfaceArea", x=>x.SurfaceArea},

    }

     

    Then I have the test method:

    [Test]
    [TestCaseSource("SpecialProperties")]
    public void SpecialProperties_Test(string specialPropertyName, System.Linq.Expressions.Expression<Func<IMyClass, double>> specialProperty)
    {
        IMyClass mockMyClass = Mock.Create<IMyClass>(Behavior.Strict);

        mockMyClass.Arrange(specialProperty).Returns(9.99);

       double result =  _concreteInstanceOnTest.MethodOnTest(specialPropertyName, mockMyClass);

        Assert.AreEqual(9.99, result);

    }

    This very nearly works, but I get an Inconsistent Accessibility error - Expression<Func<IMyclass, double>> is less accessible than SpecialProperties_Test(). I'm obviously not doing it quite right. Can anybody help?

  2. Mihail
    Admin
    Mihail avatar
    242 posts

    Posted 18 Sep 2017 Link to this post

    Hello David,

    I am afraid that I couldn't manage to reproduce the described issue.

    Would it be possible for you to open a new support ticket and provide a simple test project illustrating the problem. This way we would be able to investigate the case and assist you further.

    Regards,
    Mihail
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. David
    David avatar
    2 posts
    Member since:
    Sep 2017

    Posted 25 Sep 2017 in reply to Mihail Link to this post

    Hi Mihail

    Thanks for your response. I have managed to find a solution - I needed to work a bit harder constructing the expression for the Arrange. In case anyone reading these posts is interested, my solution looks something like this:

    [Test]
    [TestCaseSource("SpecialProperties")]
    public void SpecialProperties_Test(string specialPropertyName)
    {
        IMyClass mockMyClass = Mock.Create<IMyClass>(Behavior.Strict);
        ParameterExpression parameterExpression = Expression.Parameter(typeof(IMyClass), "x");
        Expression propertyAccess = Expression.Property(parameterExpression, specialPropertyName);
        mockPod.Arrange(Expression.Lambda<Func<IMyClass, double>>(propertyAccess, parameterExpression)).Returns(9.99);
        double result = concreteInstanceOnTest(specialPropertyName, mockMyClass);
        Assert.AreEqual(9.99, result);
        mockMyClass.AssertAll();
    }

  4. Mihail
    Admin
    Mihail avatar
    242 posts

    Posted 27 Sep 2017 Link to this post

    Hello David,

    I am glad to hear you have found a solution to the problem and thank you for sharing it with us.

    Regards,
    Mihail
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top