2017-02-10 84 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

这是问题所在。现在测试通过了。非常感谢。 –

相关问题