gpt4 book ai didi

c# - FluentAssertions 断言单个对象的多个属性

转载 作者:太空狗 更新时间:2023-10-29 17:34:47 24 4
gpt4 key购买 nike

有没有办法使用 FluentAssertions 做这样的事情

response.Satisfy(r =>
r.Property1== "something" &&
r.Property2== "anotherthing"));

我试图避免编写多个 Assert 语句。这可以通过 https://sharptestex.codeplex.com/ 实现我使用时间最长的。但是SharpTestEx不支持.Net Core。

最佳答案

.Match() 解决方案不会返回正确的错误消息。所以如果你想有一个好的错误并且只有一个断言然后使用:

result.Should().BeEquivalentTo(new MyResponseObject()
{
Property1 = "something",
Property2 = "anotherthing"
});

匿名对象(谨慎使用!)

如果您只想检查某些成员,请使用:

    result.Should().BeEquivalentTo(new
{
Property1 = "something",
Property2 = "anotherthing"
}, options => options.ExcludingMissingMembers());

Note: You will miss (new) members when testing like this. So only use if you really want to check only certain members now and in the future. Not using the exclude option will force you to edit your test when a new property is added and that can be a good thing

多个断言

Note: All given solutions gives you one line asserts. In my opinion there is nothing wrong with multiple lines of asserts as long as it is one assert functionally.

如果您想要这样做是因为您希望同时出现多个错误,请考虑将您的多行断言包装在 AssertionScope 中。

using (new AssertionScope())
{
result.Property1.Should().Be("something");
result.Property2.Should().Be("anotherthing");
}

如果它们都失败了,上面的语句现在会同时给出两个错误。

https://fluentassertions.com/introduction#assertion-scopes

关于c# - FluentAssertions 断言单个对象的多个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43544180/

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