FluentAssertions: int.Should().Equals returns wrong result?
Asked Answered
A

1

7

I just start to use Moq & FluentAssertions and find this:

results.Results.Count.Should().Equals(1);

in the code, results.Results return a list of class List. In the test setup, I set it as results.Results.Count = 3 (I can see this # is correct in debug too). But somehow, the above .Equals test passed. Then I changed it to

results.Results.Count.Should().Equals("1");

It still passed. It will fail if I use

results.Results.Count.ShouldBeEquivalentTo(1);

So, the question is:

what results.Results.Count.Should().Equals("1") compare? why it passed?

Thanks

Aerobe answered 15/6, 2016 at 21:55 Comment(2)
I don't think .Should() is a part of Moq. EDIT: yea, i can't find it in the documentationBelow
thanks. changed it.Aerobe
H
25
results.Results.Count.Should().Equals(1);

is calling the Equals method inherited from Object on the object returned by Should() which returns a bool which is being ignored. You need to use Be or Equal e.g.

results.Results.Count.Should().Be(1);
Hasid answered 15/6, 2016 at 22:13 Comment(1)
This is correct. Since FluentAssertions 6.3 this potential misuse of Equals is fixed. See changelog: github.com/fluentassertions/fluentassertions/releases/tag/6.3.0 and the issue: github.com/fluentassertions/fluentassertions/issues/1705Soloist

© 2022 - 2024 — McMap. All rights reserved.