在PrivateObject中找不到类型的构造函数错误

时间:2016-08-02 14:13:53

标签: c# unit-testing

我正在为计算软件编写一个单元测试。在测试用例中我用了" PrivateObject"用于访问私人方法" sendNumberToCalculation()" ,但我找不到类型的错误构造函数。

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

2 个答案:

答案 0 :(得分:4)

计算类不包含无参数构造函数。将构造函数参数传递给the PrivateObject constructor:

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

使用PolishNotation类型的一个参数调用sendNumberToCalculation方法,但该方法没有任何参数。从Invoke调用中删除第二个参数:

double actual = Convert.ToDouble(obj.Invoke("sendNumberToCalculation"));

答案 1 :(得分:0)

有一种更简单的方法可以实现这一点,只需先创建对象,然后调用带有对象而不是类型的PrivateObject构造函数。