2017-02-10 81 views
1

我有一個解碼給定文本的程序。它的工作原理應該如此。我創建了一個單元測試並引用了該項目。事情是,它在檢查結果是否與預期結果相同時檢測失敗,但是當我運行該項目時,結果是預期結果。單元測試沒有收到參考項目的返回值

我調試它,它證明它沒有收到引用項目的字符串的返回值。它接收加密的一個。它說Encode類的元素不公開,而且它們是。

我沒有包括Encode類,因爲我沒有那個問題。如果重要的話,這是公共靜態的。測試是不起作用的。 任何人都可以告訴我這裏有什麼問題嗎? 爲什麼它說它們不是公開的,當它們是?

+0

你沒有表現出你的編碼功能 –

+0

我也發佈了該功能,如果有幫助。 –

+0

你也可以發佈封閉類,功能看起來好嗎 –

回答

1

問題是您的密鑰是大寫字母,而您的字符串是小寫字母。所以報表

if (key[j] == toEncrypt[i]) 

else if (key[j+1] == toEncrypt[i]) 

將永遠是真實的,你應該.ToLower()在功能密鑰

編輯:

public static Tuple<string, int[]> Encode(string key, string toEncrypt) 
{ 
    key = key.ToLower(); 
    int[] iterations = new int[] { 0, 0, 0, 0, 0, 0 }; 
    if (key.Length % 2 == 0) 
    { 
     if (key.Length == key.Distinct().Count()) 
     { 
      var encodedText = new StringBuilder(toEncrypt); 
      for (int i = 0; i < toEncrypt.Length ; i++) 
      { 

       for (int j = 0; j < key.Length; j += 2) 
       { 

        if (key[j] == toEncrypt[i]) 
        { 
         encodedText[i] = key[j + 1]; 
         iterations[j/2] += 1; 

        } 
        else if (key[j+1] == toEncrypt[i]) 
        { 
         encodedText[i] = key[j]; 
         iterations[j/2] += 1; 
        } 
       } 
      } 

      return Tuple.Create(encodedText.ToString(), iterations); 
     } 
     else throw new ArgumentException("Key cannot contain the same chars"); 
    } 
    else throw new ArgumentException("You have to put a key which is dividable by 2"); 
} 
+0

這是問題所在。現在測試通過了。非常感謝。 –

相關問題