This is a migrated thread and some comments may be shown as answers.

Paramaterising the arranged property of a test

3 Answers 72 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 14 Sep 2017, 03:50 PM

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?

3 Answers, 1 is accepted

Sort by
0
Mihail
Telerik team
answered on 18 Sep 2017, 11:28 AM
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
0
David
Top achievements
Rank 1
answered on 25 Sep 2017, 12:44 PM

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();
}

0
Mihail
Telerik team
answered on 27 Sep 2017, 12:05 PM
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
Tags
General Discussions
Asked by
David
Top achievements
Rank 1
Answers by
Mihail
Telerik team
David
Top achievements
Rank 1
Share this question
or