简体   繁体   English

在 xUnit 中验证集合大小的惯用方法是什么?

[英]What's the idiomatic way to verify collection size in xUnit?

I have in my test suite a test that goes something like this:我的测试套件中有一个测试是这样的:

[Fact]
public void VerifySomeStuff()
{
    var stuffCollection = GetSomeStuff();

    Assert.Equal(1, stuffCollection.Count());
}

This test works as I expect, but when I run it xUnit prints a warning:此测试按我预期的方式工作,但当我运行它时,xUnit 会打印一条警告:

warning xUnit2013: Do not use Assert.Equal() to check for collection size.警告 xUnit2013:请勿使用 Assert.Equal() 检查集合大小。

However, no alternative is suggested in the warning, and a google search takes me to the source code in xUnit for the test that verifies this warning is printed.但是,警告中没有建议任何替代方案,谷歌搜索将我带到 xUnit 中的源代码以进行验证打印此警告的测试。

If Assert.Equal() isn't the correct way to verify the length of a collection, what is?如果Assert.Equal()不是验证集合长度的正确方法,那什么才是?


To clarify: I realize that I could "trick" xUnit into not emitting this warning by eg extracting a variable or using Assert.True(stuff.Count() == 1) instead.澄清一下:我意识到我可以通过例如提取变量或使用Assert.True(stuff.Count() == 1)来“欺骗”xUnit 不发出此警告。 The latter is just hacky, and the former feels like if xUnit is eg trying to avoid multiple iterations of an IEnumerable<T> , then this is the wrong way to go (because I'll get compiler hints about that separately if it's an issue), and xUnit itself should never have to evaluate the input more than once (in fact it probably will get the same input regardless of variable extraction, because of how C# function calling works).后者只是 hacky,前者感觉如果 xUnit 例如试图避免IEnumerable<T>的多次迭代,那么这是 go 的错误方法(因为如果这是一个问题,我会单独获得编译器提示),并且 xUnit 本身永远不必对输入求值超过一次(事实上,无论变量提取如何,它都可能获得相同的输入,因为 C# function 调用是如何工作的)。

So, I'm not just interested in removing that warning from my output. An answer to my question also explains why that warning is included in the library in the first place and why whatever approach I should use instead is better.因此,我不仅对从我的 output 中删除该警告感兴趣。对我的问题的回答还解释了为什么该警告首先包含在库中以及为什么我应该使用更好的方法。

Xunit offers quick fixes for most of its warnings, so you should be able to see what it thinks is "right". Xunit 为其大部分警告提供了快速修复,因此您应该能够看到它认为什么是“正确的”。

xunit

In your case, it wants you to use Assert.Single since you are expecting exactly one item.在您的情况下,它希望您使用Assert.Single因为您只需要一个项目。 If you were asserting an arbitrary number, like 412, then it would not give you a warning about using Count .如果您断言一个任意数字,例如 412,那么它不会向您发出有关使用Count的警告。 It will only suggest using Single if you are expecting one item, or Empty if you are expecting no items.如果您希望有一个项目,它只会建议使用Single如果您不希望有任何项目,则建议使用Empty

If you have more than one item, you can't use Assert.Single.如果您有多个项目,则不能使用 Assert.Single。

The expectation seems to be that you should use Assert.Collection :期望似乎是您应该使用Assert.Collection

var stuffCollection = GetSomeStuff();

Assert.Collection(stuffCollection, 
    item => true, // this lambda verifies the first item
    item => true, // second item
);

The assertion above verifies that there are exactly two items in the collection.上面的断言验证集合中正好有两个项目。 You can provide stricter lambdas (such as item => item.property1 == 7 ) for each item if you want.如果需要,您可以为每个项目提供更严格的 lambdas(例如item => item.property1 == 7 )。

Personally, I'm not a fan;就个人而言,我不是粉丝。 this seems like a very verbose way of saying how long you want the collection to be.这似乎是一种非常冗长的方式来表达您希望收藏的长度。

I found this give me the same error:我发现这给了我同样的错误:

Assert.Equal(2, vm.Errors.Count());

And casting it stopped the error from appearing.并投射它阻止了错误的出现。

Assert.Equal(2, (int)vm.Errors.Count());

I had same issue when I used Count property as below in xUnit.当我在 xUnit 中使用如下 Count 属性时,我遇到了同样的问题。

在此处输入图片说明

After, I use Count() function on collection, it fixed my issue.之后,我在集合上使用 Count() 函数,它解决了我的问题。

对于列表中的单个元素,最好改用它: Assert.Single(resultList);

Look I won't tell anyone if you just ignore the warning.听着,如果你忽略警告,我不会告诉任何人。 Personally I think its more hassle than its worth.我个人认为它比它的价值更麻烦。 If you have an.editorconfig:如果你有一个.editorconfig:

[*.cs]
dotnet_diagnostic.xUnit2013.severity = none # warning xUnit2013: Do not use Assert.Equal() to check for collection size.

The rule only applies when testing for 0 or 1 items in collection.该规则仅适用于测试集合中的 0 或 1 个项目。

Assert.Equal(0, result.Length) // rule warning, use .Empty
Assert.Equal(1, result.Length) // rule warning, use .Single
Assert.Equal(2, result.Length) // ok

To satisy rule:满足规则:

Assert.Empty(result); // for 0 items
Assert.Single(result); // for 1 item
Assert.NotEmpty(result); // for 2 or more items

When using Assert.NotEmpty we may as well be precise with a count使用 Assert.NotEmpty 时,我们也可以精确计数

Assert.Equal(2, result.Length) // Does not violate rule xUnit2013

https://xunit.net/xunit.analyzers/rules/xUnit2013 https://xunit.net/xunit.analyzers/rules/xUnit2013

To check the length of a collection with one element, you can use:要检查具有一个元素的集合的长度,您可以使用:

Assert.Single(yourCollection)断言。单(你的收藏)

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

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