-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ShouldBeEquivalentTo() forces array strict ordering. Should it? I think not. #25
Comments
That's an interesting statement. If we would change that, we would also need to expose some options to force strict ordening. But that all depends on how you define equivalent. |
Any workarounds? |
@dennisdoomen why would you like to add any configuration for that? Equivalence is not be equal to so order should not be taken into consideration. @LordLyng I also encountered this problem and the workaround is actually trivial. Just use EDIT
|
That's exactly what @LordLyng is proposing... |
@dennisdoomen Yes, I know. I just wanted to express my surprise for your proposal of exposing additional option. This might make sense for the sake of backward compatibility but how about publishing new major version where default behavior is aligned with the one from |
I agree, this should behave the same as the main library's BeEquivalentTo(). |
Unfortunately, this workaround doesn't work because it will be switched to the main library's BeEquaivalentTo. Like what mentioned here, false positives may occur. |
If anybody is up for a PR... |
It isn't pretty, but this workaround works for me: private void AssertJArraysEquivalentIgnoringOrder(JToken actual, JArray expected)
{
var newActual = new JArray(actual.Children().OrderBy(c => c.ToString()));
var newExpected = new JArray(expected.Children().OrderBy(c => c.ToString()));
JAssert.Should(newActual).BeEquivalentTo(newExpected);
} |
Hi @dennisdoomen, has this already been solved in a later release (and if so, what do you need to do)? Right now I have also a workaround. A bit similar like the one above but mine re-orders the json tree on a recursive way. Here is how I do it:
Make sure to include FluentAssertions.Json in your using sections |
No, the JSON version of this library didn't get much attention. In v7 of the main library, we plan to have |
I'm late to the party but I'm just suggesting that strict ordering is often the correct choice for JSON arrays as they are also used for tuples where order definitely matters. So if the behavior were to change you would definitely want an option to still enforce ordering. |
If I have an array property, the order of its values needs to be the same when asserting with
Should().BeEquivalentTo()
. I'm arguing that it shouldn't. That would be more in-line with "regular" FluentAssertions.results in
"Regular" FluentAssertions
Should().BeEquivalentTo()
does not enforce strict ordering on array properties. As such, the following test passes:This causes me problems because the data I'm comparing comes from a database and the order cannot be guaranteed. I'll need to add ordering in the service I'm testing, but that's modifying the business logic to suit the testing framework. :/
The text was updated successfully, but these errors were encountered: