开发者

MSTest - Multiple Assertions from a list

Problem: I'm trying to assert the listO开发者_Go百科fExpressions is correct.

I don't know the order the list will be returned in. I'm using MSTEST / VS2010 / C#4

    [TestMethod]
    public void GetAnswersForResultIs47()
    {
        List<string> listOfExpressions = findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(47);
        foreach (string expression in listOfExpressions)
        {
            Assert.AreEqual("1*3+5*7+9", expression);
            Assert.AreEqual("-1-3*5+7*9", expression);
        }
    }


Why not having a Dictionary of expected expressions and foreach expression returned by the method you check the dictionary for them to be, and be only once.

Something like this:

IDictionary<string, bool> CreateExpectedDictionary()
{
    IDcitionary<string, bool> expectedDictionary = new Dictionary<string, bool>();

    // Here add all the expressions you expect to be returned
    expectedDictionary.Add("1*3+5*7+9", false);
    expectedDictionary.Add("-1-3*5+7*9", false);

    return expectedDictionary ;
}

[TestMethod]
public void GetAnswersForResultIs47()
{
    List<string> listOfExpressions = findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(47);
    Dictionary<string, bool> expectedDic = CreateExpectedDictionary();

    foreach (string expression in listOfExpressions)
    {
         if(expectedDic.ContainsKey(expression))
         {
             if(expectedDic[expression])
             {
                 Assert.Fail(String.Format("The expression {0} was returned more than once.",expression));
             }

             expectedDic[expression] = true;
         }
         else
         {
             Assert.Fail(String.Format("The expression {0} was not expected", expression));  
         }
    }

    foreach(string exp in expectedDic.Keys)
    {
        if(!expectedDic[exp])
        {
            Assert.Fail(String.Format("The expression {0} is missing.", exp));
        }
    }
}   


Maybe you could try this...

[TestMethod]
        public void GetAnswersForResultIs47()
        {
            List<string> listOfExpressions = findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(47);
            Assert.AreEqual(true, listOfExpressions.Contains("1*3+5*7+9"));
            Assert.AreEqual(true, listOfExpressions.Contains("-1-3*5+7*9"));
        }


As you're not sure of the order, use CollectionAssert.AreEquivalent.

Verifies that the specified collections are equivalent. Two collections are equivalent if they have the same elements in the same quantity, but in any order. Elements are equal if their values are equal, not if they refer to the same object.

For example:

    [TestMethod]
    public void GetAnswersForResultIs47()
    {
        List<string> listOfExpressions = findArithmeticSymbolsInNumericOrder13579ThatGivesThisResult(47);
        List<string> expected = new List<string> { "1*3+5*7+9", "-1-3*5+7*9" };
        CollectionAssert.AreEquivalent(expected, listOfExpressions);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜