- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从我们的数据库中,我们查询放在 ExpandoObject
类型的动态对象集合中的记录集。实现 IDictionary<string, object>
在田野上。这些是实际值。
从我们的 SpecFlow BDD 测试中,我们得到了一组实现 IDictionary<string, string>
的 TableRows .这些是我们的预期值。
使用 FluentAssertions,我们想用 actual.Should().BeEquivalentTo(expected)
测试整个集合的等效性.不幸的是,这行不通,因为当实际值不是 string
类型时类型不匹配.
我们可以使用 actual.Should().BeEquivalentTo(expected, options => options.WithAutoConversion())
, 但这将使整个实际集合成为 IDictionary<string, string>
的集合这对于比较日期没有用。
我组装了一个将显示相同问题的测试用例:
var expected = new List<Dictionary<string, string>>();
expected.Add(new Dictionary<string, string>
{
{"Name", "Moon Inc."},
{"Number", "42"},
{"Date", "2018-12-31"}
});
var actual = new List<ExpandoObject>();
dynamic eo = new ExpandoObject();
eo.Name = "Moon Inc.";
eo.Number = 42;
eo.Date = new DateTime(2018, 12, 31);
actual.Add(eo);
actual.Should().BeEquivalentTo(expected, options => options);
/*
This throws:
NUnit.Framework.AssertionException:
Expected item[0][Number] to be System.String, but found System.Int32.
Expected item[0][Date] to be System.String, but found System.DateTime.
*/
actual.Should().BeEquivalentTo(expected, options => options.WithAutoConversion());
/*
This throws:
NUnit.Framework.AssertionException:
Expected item[0][Date] to be "2018-12-31" with a length of 10,
but "31-12-2018 0:00:00" has a length of 18.
/*
我尝试在如下使用方法中使接收类型动态化:
actual.Should().BeEquivalentTo(expected, options => options
.Using<dynamic>(ctx => ctx.Subject.Should().Be(ctx.Expectation)).WhenTypeIs<DateTime>()
.Using<dynamic>(ctx => ctx.Subject.Should().Be(ctx.Expectation)).WhenTypeIs<int>());
/*
NUnit.Framework.AssertionException:
Expected item[0][Number] to be System.String, but found System.Int32.
Expected item[0][Date] to be System.String, but found System.DateTime.
*/
将双方解析为 DateTime
并且使用自动转换也不起作用,因为 actual
类型不被视为 DateTime
但作为 string
:
actual.Should().BeEquivalentTo(expected, options => options
.Using<dynamic>(
ctx =>
DateTime.ParseExact(ctx.Subject, "yyy-MM-dd", CultureInfo.InvariantCulture)
.Should().Be(DateTime.ParseExact(ctx.Expectation, "yyy-MM-dd", CultureInfo.InvariantCulture)))
.WhenTypeIs<DateTime>()
.WithAutoConversion());
/*
NUnit.Framework.AssertionException:
Expected item[0][Date] to be "2018-12-31" with a length of 10,
but "31-12-2018 0:00:00" has a length of 18.
*/
FluentAssertions 有什么方法可以实现这一点吗?
最佳答案
如果没有其他帮助,您可以像这样实现自定义 IEquivalencyStep
:
class WeakDateEquivalencyStep : IEquivalencyStep {
public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) {
if (context.IsRoot)
return false;
// handles situations when subject is date
// but expectation is string
return context.Subject is DateTime && context.Expectation is string;
}
public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config) {
DateTime exp;
// we know that expection is string here
if (!DateTime.TryParse((string) context.Expectation, CultureInfo.InvariantCulture, DateTimeStyles.None, out exp)) {
// do something, your spec is invalid
throw new Exception($"Value {context.Expectation} does not represent valid date time");
}
context.Subject.Should().Be(exp, context.Because, context.BecauseArgs);
return true;
}
}
然后
actual.Should().BeEquivalentTo(expected, options =>
options.Using(new WeakDateEquivalencyStep()).WithAutoConversion());
关于c# - 使用 FluentAssertions 比较包含不同类型的两个字典集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49850604/
如果我下载 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
我是一名优秀的程序员,十分优秀!