gpt4 book ai didi

c# - FluentAssertion 无法比较可枚举的字符串

转载 作者:行者123 更新时间:2023-11-30 19:46:10 25 4
gpt4 key购买 nike

这段代码工作正常

    [Test]
public void boo()
{
var collection = new[] { 1, 2, 3 };
collection.Should().Equal(1, 2, 3);
}

但是,这失败了

    [Test]
public void foo()
{
var collection = new[] { "1", "2", "3" };
collection.Should().Equal("1", "2", "3");
}

失败信息是:

'Expected collection to be equal to {1} because 2, but {"1", "2", "3"} contains 2 item(s) too many.'

这里有什么问题?为什么无法比较字符串的可枚举?

当然,我的问题是 - 如何处理 foo() 中的大小写?

最佳答案

问题是第二次调用被解析为以下重载:

public AndConstraint<TAssertions> Equal(IEnumerable expected, 
string reason,
params object[] reasonArgs);

代替:

public AndConstraint<TAssertions> Equal(params object[] elements);

要获得所需的结果,您可以强制编译器使用正确的重载方法,例如:

collection.Should().Equal((object)"1", "2", "3");

关于c# - FluentAssertion 无法比较可枚举的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9118143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com