2010-12-17 65 views
3

我试图测试功能如何测试函数返回的IEnumerable <Integer>

public static IEnumerable<Integer> Divisors(Integer n) 
{ 
    int max = (int)System.Math.Sqrt(n); 

    if (n != 0) 
     yield return 1; 

    for (int i = 2; i <= max; i++) 
     if (n % i == 0) 
      yield return i; 
} 

,我需要写这样

[Test] 
public void DivisorsTest() 
{ 
    Integer n = 0; 
    IEnumerable<Integer> expected = 0 ; //error 
    IEnumerable<Integer> actual; 
    actual = Science.Mathematics.NumberTheoryFunctions.Divisors(n); 
    Assert.AreEqual(expected, actual); 
} 

测试功能如何修改这条线测试输出我需要测试不仅是零的返回值

+10

欢迎来到StackOverflow!为了将来的参考,一个写得很好(并且格式良好)的问题并不要求“即将可用”的答案将会更加有利。 – Donut 2010-12-17 21:37:59

+0

顺便说一句,你使用的UT框架? NUnit,VS单元测试? – 2010-12-17 22:43:02

+0

我正在使用NUnit框架 – Emykindman 2010-12-17 23:29:06

回答

0

OK,谢谢大家对你的答案,这里是我的所作所为使用你的答案

测试功能将是: 1,当你想到的功能赢得了” t返回任何值

[Test] 
    public void DivisorsTest_01() 
    { 
     Integer n = 0; 
     IEnumerable<Integer> actual; 
     actual = Science.Mathematics.NumberTheoryFunctions.Divisors(n); 
     Assert.IsFalse(actual.Any()); // There should not be any elements returned so empty 
    } 

2 - 所有你需要的是在O/P转换到一个数组并使用它:

[Test] 
    public void DivisorsTest_03() 
    { 
     Integer n = 9; 
     Integer[] expected = new Integer[3] { 1,3,9 }; 
     IEnumerable<Integer> actual; 
     actual = Science.Mathematics.NumberTheoryFunctions.Divisors(n); 
     var actual1 = actual.ToArray(); 

     Assert.AreEqual(expected[0], actual1[0]); 
     Assert.AreEqual(expected[1], actual1[1]); 
     Assert.AreEqual(expected[2], actual1[2]); 

    } 

3-有时你期望的输出将是例外,所以不要忘了写:

[Test] 
[ExpectedException] 

之前的功能。

再次感谢大家

2

检查整个集合有各种技巧。它看起来像的零输入,你不希望有任何返回的值(即空集),所以你可以这样做:

​​

对于更复杂的输出,它往往更容易把结果到一个数组中,并通过指数检查结果是这样的:

var results = actual.ToArray(); 
Assert.AreEqual(5, results.Count); 
Assert.AreEqual(1, results[0]); 
Assert.AreEqual(2, results[1]); 
// etc. 
0

你不能使用这样的(对不起,它需要LINQ):

[Test] 
public void DivisorsTest() 
{ 
    int n = 0; 
    int expected = 0; //error 
    IEnumerable<Integer> actual; 
    actual = Science.Mathematics.NumberTheoryFunctions.Divisors(n); 
    Assert.IsTrue(actual.All(x => x != expected)); 
} 
+0

预期不仅仅是零值,只是可检验的,下一个测试的情况将是预期的值3,9,12,15 – Emykindman 2010-12-17 22:22:44

+0

运算符'!='不能应用于类型为'Science.Integer'和'Science'的操作数.Integer []' – Emykindman 2010-12-19 04:40:35

+0

是的,我的示例旨在针对单个整数测试“实际”数组,因此我同意您的看法,这不是一个解决方案。但@Thomas Levesque给出了一个很好的解决方法,应该解决您的问题。 – 2010-12-19 06:53:05

0

你也可以初始化预期到一个空的枚举erable(即零元)这样

IEnumerable<int> expected = System.Linq.Enumerable.Empty<int>(); 

MSDN Reference for Enumerable

+0

其实我也想测试其他情况不只是零情况,因为返回值可能不只是一个值 – Emykindman 2010-12-17 22:20:53

+0

有多个测试呢?一个为零,一个为零。只是一个想法。 – miko 2010-12-17 22:39:12

+0

我希望是一个数组包含值不是空的..你会告诉我如何 – Emykindman 2010-12-19 04:18:53

0

我经常使用类似的东西:

var expected = new int[] { /* expected values */ }; 
var actual = Science.Mathematics.NumberTheoryFunctions.Divisors(n); 
Assert.IsTrue(expected.SequenceEquals(actual)); 

的缺点是断言默认的错误消息还不是很描述:

取而代之,您可以使用CollectionAssert.AreEquivalent ,这给出了更详细的错误信息,但它并不理想要么...如果你使用LINQ查询,该消息可以看起来像:

Expected: equivalent to < 0, 1, 3, 4, 6, 7, 9, 10 > 
But was: <System.Linq.Enumerable+<WhereIterator>d__0`1[System.Int32]> 

(至少NUnit的2.4.8,也许我应该升级到新版本?)