代码之家  ›  专栏  ›  技术社区  ›  Sarah Vessels

C#单元测试中的双向列表比较

  •  5
  • Sarah Vessels  · 技术社区  · 15 年前

    在我的C#单元测试中,我经常根据ID列表查询行列表。然后我想确保1)对于所有ID,至少找到一行具有该ID;2)对于所有返回的行,每行都有一个ID,该ID位于要查找的ID列表中。以下是我通常如何确保:

    Assert.IsTrue(ids.All(
        id => results.Any(result => result[primaryKey].Equals(id))
    ), "Not all IDs were found in returned results");
    
    Assert.IsTrue(results.All(
        result => ids.Any(id => result[primaryKey].Equals(id))
    ), "Returned results had unexpected IDs");
    

    我认为 Any All

    编辑:

    string[] ids1 = { "a", "b", "c" };
    string[] ids2 = { "b", "c", "d", "e" };
    string[] ids3 = { "c", "a", "b" };
    Assert.AreEqual(
        1,
        ids1.Except(ids2).Count()
    );
    Assert.AreEqual(
        2,
        ids2.Except(ids1).Count()
    );
    Assert.AreEqual(
        0,
        ids1.Except(ids3).Count()
    );
    
    4 回复  |  直到 15 年前
        1
  •  4
  •   Aviad P.    15 年前

    您可以选择使用 Except 接线员:

    var resultIds = results.Select(x => x[primaryKey]);
    
    Assert.IsTrue(resultIds.Except(ids).Count() == 0,
     "Returned results had unexpected IDs");
    
    Assert.IsTrue(ids.Except(resultIds).Count() == 0,
     "Not all IDs were found in returned results");
    
        2
  •  3
  •   Carl Manaster    15 年前

    在国际海事组织,它的可读性不如它所能。创建并记录一个返回true/false的方法。然后调用Assert.IsTrue(methodWithDescriptiveNameWhichReturnsTrueOrfalse(),“失败原因”);

        3
  •  1
  •   Arthis    13 年前

    下面是我为处理两个可枚举项而编写的代码片段,在MS test中执行单元测试时引发异常,这可能会有所帮助:

    比较两个可枚举项:

     MyAssert.AreEnumerableSame(expected,actual);
    

    管理异常

    MyAssert.Throws<KeyNotFoundException>(() => repository.GetById(1), string.Empty);
    

    代码

    public class MyAssert
        {
            public class AssertAnswer
            {
                public bool Success { get; set; }
                public string Message { get; set; }
            }
    
            public static void Throws<T>(Action action, string expectedMessage) where T : Exception
            {
                AssertAnswer answer = AssertAction<T>(action, expectedMessage);
    
                Assert.IsTrue(answer.Success);
                Assert.AreEqual(expectedMessage, answer.Message);
            }
    
            public static void AreEnumerableSame(IEnumerable<object> enumerable1, IEnumerable<object> enumerable2)
            {
                bool isSameEnumerable = true;
                bool isSameObject ;
    
                if (enumerable1.Count() == enumerable2.Count())
                {
                    foreach (object o1 in enumerable1)
                    {
                        isSameObject = false;
                        foreach (object o2 in enumerable2)
                        {
                            if (o2.Equals(o1))
                            {
                                isSameObject = true;
                                break;
                            }
                        }
                        if (!isSameObject)
                        {
                            isSameEnumerable = false;
                            break;
                        }
                    }
                }
                else
                    isSameEnumerable = false;
    
                Assert.IsTrue(isSameEnumerable);
            }
    
            public static AssertAnswer AssertAction<T>(Action action, string expectedMessage) where T : Exception
            {
                AssertAnswer answer = new AssertAnswer();
    
                try
                {
                    action.Invoke();
    
                    answer.Success = false;
                    answer.Message = string.Format("Exception of type {0} should be thrown.", typeof(T));
                }
                catch (T exc)
                {
                    answer.Success = true;
                    answer.Message = expectedMessage;
                }
                catch (Exception e)
                {
                    answer.Success = false;
                    answer.Message = string.Format("A different Exception was thrown {0}.", e.GetType());
                }
    
                return answer;
            }
        }
    
        4
  •  0
  •   Peter Seale    15 年前

    CollectionAssert 有助于可读性的断言系列。