简体   繁体   English

Assert.Empty(msgs)和Assert.False(msgs.Any())有什么区别?

[英]What is the difference between Assert.Empty(msgs) and Assert.False(msgs.Any())?

I am using XUnit to test for scenarios where an empty Enumerable list is expected. 我正在使用XUnit来测试需要空的Enumerable列表的场景。

I have noticed that in certain scenarios: 我注意到在某些情况下:

  • Assert.Empty(msgs); fails; 失败;

BUT

  • Assert.False(msgs.Any()); is passing. 正在过去。

This is a bit confusing to me as I anticipated that this was testing for the same thing. 这对我来说有点混乱,因为我预计这会测试同样的事情。

I understand that this likely because of the differences in expected behaviour between: 我理解这可能是因为预期行为的差异:

  1. Enumerable.Any() (which defines this as "Determines whether a sequence contains any elements.") Enumerable.Any() (将其定义为“确定序列是否包含任何元素。”)

AND

  1. The empty expected in XUnit.Empty() (which defines that this is testing for an empty Object). XUnit.Empty()中预期的空(定义这是测试空对象)。

However, I am not sure exactly the difference as it appeared to me to be essentially testing the same thing. 但是,我不确定区别,因为在我看来基本上测试同样的东西。

Could someone please explain the differences in what is being tested for in these two different types of Asserts? 有人可以解释在这两种不同类型的断言中测试的差异吗?

Here is the source for Enumerable.Any (The Assert.False() just validates that this returns false .): 这是Enumerable.Any的源代码( Assert.False()只验证它返回false 。):

public static bool Any<TSource>(this IEnumerable<TSource> source) {
    if (source == null) throw Error.ArgumentNull("source");
    using (IEnumerator<TSource> e = source.GetEnumerator()) {
        if (e.MoveNext()) return true;
    }
    return false;
}

Here is the source for Assert.Empty from xUnit: 以下是来自xUnit的Assert.Empty的源代码:

public static void Empty(IEnumerable collection)
{
    Assert.GuardArgumentNotNull("collection", collection);

    var enumerator = collection.GetEnumerator();
    try
    {
        if (enumerator.MoveNext())
            throw new EmptyException(collection);
    }
    finally
    {
        (enumerator as IDisposable)?.Dispose();
    }
}

They seem to be using a very similar way of checking for the presence of items in the collection. 他们似乎使用一种非常类似的方式来检查集合中是否存在项目。 I'd expect the same result from each method. 我希望每种方法都能得到相同的结果。

Without more details about how you are using each one, it's hard to say why you are getting different results. 如果没有关于如何使用每一个的更多细节,很难说为什么你会得到不同的结果。

msge.Any() returns true when msge is not null and have one or more element and false otherwise so probebly msge is null and Assert.Empty fails when argument is null. 当msge不为null并且有一个或多个元素时,msge.Any()返回true,否则返回false,因此当参数为null时,msll为null,Assert.Empty失败。
With the best regards. 最诚挚的问候。

There is a difference between these two methods: 这两种方法有所不同:

.Any() is an extension method which takes an IEnumerable - An object an implement the IEnumerable interface to let the code iterate through it for set operations, (like .Any() or .Where()) .Any()是一个扩展方法 ,它接受一个IEnumerable - 一个对象一个IEnumerable接口的实现,让代码遍历它进行集合操作,(如.Any()或.Where())

Assert.Empty() doesn't appear to check whether an object implements IEnumerable, but only checks against an empty set if the input data is a string or an array. Assert.Empty()似乎不检查对象是否实现IEnumerable,而只检查输入数据是字符串还是数组时对空集的检查。

My guess then is that you're passing in an IEnumerable object, rather than an array. 我的猜测是你传入一个IEnumerable对象,而不是一个数组。

To get around this you could either use Assert.False(msgs.Any()); 要解决这个问题,你可以使用Assert.False(msgs.Any()); as before, or else use something like Assert.Empty(msgs.ToArray()); 像以前一样,或者使用像Assert.Empty(msgs.ToArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM