如果一个属性可以为空,则比较对象时 FluentAssertions 失败
Posted
技术标签:
【中文标题】如果一个属性可以为空,则比较对象时 FluentAssertions 失败【英文标题】:FluentAssertions fails when comparing objects if one property is nullable 【发布时间】:2013-11-11 21:43:36 【问题描述】:场景:我有一个具有可为空属性的对象,当我运行被测方法时,该对象将被更新。在预期的对象中,我没有指定它,因为我想单独验证该值。这是一个简单的测试演示
using System;
using FluentAssertions;
using NUnit.Framework;
namespace FluentAssertionsNullableFailure
public class SimpleWithNullable
public Int64? nullableIntegerProperty
get; set;
public string strProperty
get; set;
[TestFixture]
public class Demo
public SimpleWithNullable actual = new SimpleWithNullable nullableIntegerProperty = 1, strProperty = "I haz a string!" ;
public SimpleWithNullable expected = new SimpleWithNullable strProperty = "I haz a string!" ;
[Test]
public void NullableTest ()
actual.ShouldBeEquivalentTo (
expected,
opt => opt.Using<Int64?> ( c => c.Subject.Should ().BeInRange ( 0, 10 ) ).WhenTypeIs<Int64?> ()
);
但是,这会失败并显示以下消息:
Test Name: NullableTest
Test FullName: FluentAssertionsNullableFailure.Demo.NullableTest
Test Source: c:\Users\ebelew\Documents\Visual Studio 2012\Projects\FluentAssertionsNullableFailure\FluentAssertionsNullableFailure\Demo.cs : line 25
Test Outcome: Failed
Test Duration: 0:00:00.271
Result Message:
Expected property nullableIntegerProperty to be <null>, but found 1.
With configuration:
- Select all declared properties
- Match property by name (or throw)
- Invoke Action<Nullable`1> when info.RuntimeType.IsSameOrInherits(System.Nullable`1[System.Int64])
- Invoke Action<DateTime> when info.RuntimeType.IsSameOrInherits(System.DateTime)
- Invoke Action<String> when info.RuntimeType.IsSameOrInherits(System.String)
Result StackTrace:
at FluentAssertions.Execution.LateBoundTestFramework.Throw(String message) in d:\Workspace\Github\FluentAssertions\FluentAssertions.Net35\Execution\LateBoundTestFramework.cs:line 25
at FluentAssertions.Execution.CollectingAssertionStrategy.ThrowIfAny(IDictionary`2 context) in d:\Workspace\Github\FluentAssertions\FluentAssertions.Net35\Execution\CollectingAssertionStrategy.cs:line 57
at FluentAssertions.Execution.AssertionScope.Dispose() in d:\Workspace\Github\FluentAssertions\FluentAssertions.Net35\Execution\AssertionScope.cs:line 267
at FluentAssertions.Equivalency.EquivalencyValidator.AssertEquality(EquivalencyValidationContext context) in d:\Workspace\Github\FluentAssertions\FluentAssertions.Net35\Equivalency\EquivalencyValidator.cs:line 55
at FluentAssertions.AssertionExtensions.ShouldBeEquivalentTo[T](T subject, Object expectation, Func`2 config, String reason, Object[] reasonArgs) in d:\Workspace\Github\FluentAssertions\FluentAssertions.Net35\AssertionExtensions.cs:line 497
at FluentAssertionsNullableFailure.Demo.NullableTest() in c:\Users\ebelew\Documents\Visual Studio 2012\Projects\FluentAssertionsNullableFailure\FluentAssertionsNullableFailure\Demo.cs:line 25
如何使用 Options 上下文来执行此操作,而不是将其拆分为两个或多个断言。 (一个断言用于公共属性,每个用于具有范围或期望值而不是严格等价的项目)
注意:这在 expected
对象中没有显式值的引用类型上也会失败。
注意+:我试过 ExcludingMissingProperties(),它不会改变错误。
【问题讨论】:
【参考方案1】:您发现了一个我没有预见到的情况。我将在 2.2 版中修复它,但您可以通过 https://github.com/dennisdoomen/fluentassertions/issues/33 跟踪我的进度
【讨论】:
问题已解决。请查看 GitHub 上的问题以获取更多详细信息。以上是关于如果一个属性可以为空,则比较对象时 FluentAssertions 失败的主要内容,如果未能解决你的问题,请参考以下文章
Mapstruct 映射:如果所有源参数属性为空,则返回空对象