Click here to Skip to main content
15,886,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm writing a unit test for calculation software.in test case i used of "PrivateObject" for accessing to private method "sendNumberToCalculation()" , but i get error constructor on type not found.

C#
 public class CalculationTest
{
    [TestMethod]
    public void sendNumberToCalculationTest()
    {

        // -- Act
        PrivateObject obj = new PrivateObject(typeof(Calculation));
        Tokenization token = new Tokenization("5*10-18/(3+19)");
        PolishNotation polish = new PolishNotation(token.MathExpressionParser());

        double expected = 49.19;

        // -- Actual
        double actual = Convert.ToDouble(obj.Invoke("sendNumberToCalculation", polish));

        // -- Assert
        Assert.AreEqual(expected, actual);
    }
}

    public class Calculation
{

    private Tokenization token;

    private PolishNotation polish;
    private Stack<double> numbers = new Stack<double>();
    private Stack<string> operators = new Stack<string>();

    public Calculation(string expression)
    {
        token = new Tokenization(expression);
        polish = new PolishNotation(token.MathExpressionParser());
    }

    private double sendNumberToCalculation()
    {
        int number;
        int number1 = 0;
        int number2 = 0;
        string operatorName = "";
        int counter = 1;
        foreach (var item in polish.InfixToPostfix())
        {
            numbers.Push(Convert.ToDouble(item));
            if (!int.TryParse(item, out number))
            {
                operators.Push(item);
                while (counter <= 2 && numbers.Count > 1)
                {
                    if (counter == 1)
                    {
                        number2 = Convert.ToInt32(numbers.Pop());
                    }
                    else if (counter == 2 && operators.Count > 0)
                    {
                        number1 = Convert.ToInt32(numbers.Pop());
                        operatorName = operators.Pop();
                    }
                }
            }
            operatorDetect(number1, number2, operatorName);
        }
        var result = numbers.Pop();

        return result;
    }

    private void operatorDetect(int number1, int number2, string operatorName)
    {
        switch (operatorName)
        {
            case "+":
                Add(number1, number2);
                break;

            case "*":
                Multipy(number1, number2);
                break;

            case "/":
                Divide(number1, number2);
                break;

            case "-":
                Subtract(number1, number2);
                break;
        }
    }

    private void Add(int number1, int number2)
    {
        double number = number1 + number2;
        numbers.Push(number);
    }

    private void Multipy(int number1, int number2)
    {
        double number = number1 * number2;
        numbers.Push(number);
    }

    private void Subtract(int number1, int number2)
    {
        double number = number1 / number2;
        numbers.Push(number);
    }

    private void Divide(int number1, int number2)
    {
        double number = number1 - number2;
        numbers.Push(number);
    }
}


What I have tried:

i get error constructor on type not found and i wanna fix it bug.
Posted
Updated 2-Aug-16 9:05am

Your Calculation class does not contain a parameterless constructor. You need to pass the constructor parameters to the PrivateObject constructor[^]:
C#
string expression = "5*10-18/(3+19)";

PrivateObject obj = new PrivateObject(
    typeof(Calculation),      // The type of the object to create
    new[] { typeof(string) }, // The type of each parameter
    new[] { expression });    // The value for each parameter

You're also trying to invoke the sendNumberToCalculation method with one parameter of the PolishNotation type, but that method doesn't have any parameters. You need to remove the second argument from the Invoke call:
C#
double actual = Convert.ToDouble(obj.Invoke("sendNumberToCalculation"));
 
Share this answer
 
Comments
log98 2-Aug-16 15:27pm    
Richard,thanks so much,do you can explain a little bit more? in fact i don't know about new[] typeof(string) and new[] { expression} .
Richard Deeming 3-Aug-16 8:00am    
Those are implicitly-typed array initializers.

You could also write them as new Type[1] { typeof(string) } and new object[1] { expression }.

Or the really verbose option:
Type[] parameterTypes = new Type[1];
parameterTypes[0] = typeof(string);
object[] parameterValues = new object[1];
parameterValues[0] = expression;
PrivateObject obj = new PrivateObject(typeof(Calculation), parameterTypes, parameterValues);



(Technically, new[] { expression } creates a new string array, rather than an object array. But arrays are covariant[^], so it still works.)
Can we have a screenshot of the error and the line please ?
 
Share this answer
 
Comments
Richard Deeming 2-Aug-16 14:58pm    
If you have a question or comment, click the "Have a Question or Comment?" button under the question.

Do not post your comment as a solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900