2016-08-02 77 views
3

我正在編寫一個計算軟件的單元測試。我使用「PrivateObject」訪問私有方法「sendNumberToCalculation()」的測試用例,但是我找到類型未找到的錯誤構造函數。構造函數類型未找到在PrivateObject中的錯誤

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

可能有助於http://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method – Vladimir

回答

2

計算類不包含無參數構造函數。給構造參數傳遞給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 

與波蘭表示法類型的一個參數來調用sendNumberToCalculation方法,但是該方法不具有任何參數。從invoke調用刪除第二個參數:

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

還有就是要做到這一點更簡單的方法,只需先創建對象,然後調用PrivateObject構造函數的對象,而不是一個類型的。

相關問題