fluentassertions icon indicating copy to clipboard operation
fluentassertions copied to clipboard

Provide value in assertion failure message for variable expectation

Open sw-carlin opened this issue 8 years ago • 10 comments

When I include a class field in an assertion I sometimes see something like:

Collection {empty} should have an item matching (x.MyProperty == value(TestClassName).MyClassField)

It seems like FA attempted to read the value of the field but failed figuring out how to do that properly.

The assertion would look something like this:

var myCollection = List<boolean?>();
myCollection.Should().Contain(x => x.HasValue == MyClassFlield.HasValue)

sw-carlin avatar Jan 25 '18 23:01 sw-carlin

Thanks for reporting. Which version of FA? 5.0 RC2?

And is that the actual assertion code that you used?

dennisdoomen avatar Jan 26 '18 05:01 dennisdoomen

Think I found the same problem ` [Test] public void Demo() { var list = new List { 1, 2 };

        var myNumber = 3;

        list.Should().Contain(x => x == myNumber);
    }

` results in

Expected list {1, 2} to have an item matching (x == value(FluentTesting.TestintFluentAssertion+<>c__DisplayClass0_0).myNumber). at FluentAssertions.Execution.LateBoundTestFramework.Throw(String message) at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args) at FluentAssertions.Collections.SelfReferencingCollectionAssertions2.Contain(Expression1 predicate, String because, Object[] becauseArgs) at FluentTesting.TestintFluentAssertion.Demo()

The same goes for filtering on properties like somelist.Should().Contain(x => x.MyProperty == foo)

nbrannstrom avatar Feb 23 '18 07:02 nbrannstrom

Ah, that's FA just trying to render a display version of the expression. Nothing more.

dennisdoomen avatar Feb 23 '18 07:02 dennisdoomen

Does that answer your question?

dennisdoomen avatar Feb 28 '18 18:02 dennisdoomen

It would be nice if the result showed the actual value on 'myNumber' in the example above. The same way it shows 3 when you use the following ` var list = new List { 1, 2 };

    list.Should().Contain(x => x == 3);

`

nbrannstrom avatar Mar 01 '18 07:03 nbrannstrom

Honestly, I would not know how to accomplish that.

dennisdoomen avatar Mar 01 '18 18:03 dennisdoomen

The compiled Expression uses the value of myNumber at invocation time.

int myNumber = 1;
Expression<Func<int, bool>> act = i => i == myNumber;
act.Body.ToString; // "(i == value(Submission#16).<Submission#15>.myNumber)"
var f = act.Compile();

f(1); // true
f(2); // false

myNumber = 2;
f(1); // false
f(2); // true

If you instead make myNumber const, the Expression can utilize the constness.

const int myNumber = 1;
Expression<Func<int, bool>> act = i => i == myNumber;
act.Body.ToString; // "(i == 3)"

jnyrup avatar Mar 01 '18 18:03 jnyrup

@sw-carlin do these answers answer your question?

jnyrup avatar Mar 24 '18 12:03 jnyrup

Yes @jnyrup , that is the behavior I am seeing. I thought FA would be able to access the variable value for the error message but apparently it can only do that for literal values.

sw-carlin avatar May 06 '18 04:05 sw-carlin

Or you just call list.Should().Contain(myNumber);

dennisdoomen avatar May 06 '18 06:05 dennisdoomen