- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
有没有办法使用 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");
}
如果它们都失败了,上面的语句现在会同时给出两个错误。
关于c# - FluentAssertions 断言单个对象的多个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43544180/
如果我下载 FluentAssertions 1.7.1(我的所有单元测试当前都是针对它编写的),那么我可以引用该 dll,并且下面的测试将正常工作。 但我尝试通过 NuGet 并使用 FluentA
我一直在使用 FluentAssertions 进行单元测试,并开始研究断言是否正确抛出异常。我知道我可以使用 ExpectedExceptions 方法属性,但如果可能的话,我想学习 FluentA
这段代码工作正常 [Test] public void boo() { var collection = new[] { 1, 2, 3 };
我有这样的单元测试代码: // version3 should be a copy of version2 with some differences version3.Data.ShouldBeEq
FluentAssertions 中检查两个对象不相等的最佳方法是什么,如果类没有实现 Equals 而我想用反射进行逐字段比较?基本上我想要 Should().BeEquivalentTo() 的对
我有一个嵌套的 class和 FluentAssertions可以断言它们。 然后我改变class至 struct并且测试失败。 (如果我将 IEnumerable MyItems { get; se
我尝试使用 FluentAssertions 检查我的单元测试,项目列表中的属性类型属于特定类型。 myObj.Items.OfType().Single() .MyPrope
有没有办法使用 FluentAssertions 做这样的事情 response.Satisfy(r => r.Property1== "something" && r.Propert
我正在将 FluentAssertions 与 NUnit 一起使用,但我意识到 Throw() 方法和其他相关方法并未列出供我使用。我是否必须安装任何其他软件包才能访问此方法? 我使用的是 NuGe
因为我有一些角度,所以我想检查角度模数 360°: double angle = 0; double expectedAngle = 360; angle.Should().B
我想知道如何使用 Fluent Assertions 检查一个对象是否从另一个类继承? 我知道我可以使用 IsAssignableFrom 使用 xUnit 来做到这一点,如下所示: [Fact] p
因为我有一些角度,所以我想检查角度模数 360°: double angle = 0; double expectedAngle = 360; angle.Should().B
从我们的数据库中,我们查询放在 ExpandoObject 类型的动态对象集合中的记录集。实现 IDictionary在田野上。这些是实际值。 从我们的 SpecFlow BDD 测试中,我们得到了一
可能是一个简单的,但无法让它工作; 我已将方法上的签名一更改为任务 在我的单元测试中,我使用了流畅的断言。 但无法让它工作: _catalogVehicleMapper
我有以下规范 BidirectionalGraph Fixture = new BidirectionalGraph(); public void VerticesShouldBeAbleToAsso
我尝试不仅将 FluentAssertions 用作测试断言框架,而且还用于检查运行时契约(如高级 Debug.Assert,我已阅读 this 问题)。期望的行为是: 在 Debug模式下:抛出异常
如何比较具有名称相同但类型不同的属性的对象? public class A { public Guid Id { get; set; } } public class B { publ
我正在尝试使用最新版本的 FluentAssertions (4.0.1) 更新我的 Unittest 项目,但由于 API 的更改,我的测试不再编译。在更新之前,我使用的是 3.4.1 版,以下代码
我有一个包含各种类型的字段/属性等的类: public string SomeStringData; public int? SomeNullableIntegerData; public SomeD
当我遇到一个我预计会失败但实际通过的测试时,我正在为一个实用程序库编写一些单元测试。该问题涉及比较两个 float 变量,而不是比较一个 float? 和一个 float 变量。 我使用的是 NUni
我是一名优秀的程序员,十分优秀!