Question

Scenario: I have an object with a nullable property that will be updated when I run the method under test. In the expected object, I don't specify it, since I want to validate the value separately. Here is a simple test demonstration

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?> ()
            );
        }
    }
}

However, this fails with the following message:

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

How can I do this with the Options context, as opposed to splitting it into two or more assertions. (One assert for common properties, one each for item with a range or desired value instead of strict equivalence)

Note: This also fails on reference types without an explicit value in the expected object.

Note+: I've tried ExcludingMissingProperties(), it doesn't change the error.

Was it helpful?

Solution

You've uncovered a scenario that I didn't foresee. I'll fix it in release 2.2, but you can track my progress via https://github.com/dennisdoomen/fluentassertions/issues/33

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top