-
Notifications
You must be signed in to change notification settings - Fork 29
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
Feature request: Allow using the working comparison even when JTokens are nested #61
Comments
It seems what you're looking for is a combination of what |
Yes that is what I'm looking for, I assumed this project would be the right place for it given its name and the fact that it already combines FluentAssertion with Newtonsoft's JToken, albeit only for top-level comparisons :). |
You can extend the equivalency comparisons with the [TestMethod]
public void LocalOptions()
{
var d = new Dictionary<string, JToken> { ["a"] = 5 };
var e = new Dictionary<string, JToken> { ["a"] = 7 };
d.Should().BeEquivalentTo(e, opt => opt
.Using<JToken>(ctx => ctx.Subject.Should().BeEquivalentTo(ctx.Expectation))
.WhenTypeIs<JToken>()
);
}
[TestMethod]
public void GlobalOptions()
{
AssertionOptions.AssertEquivalencyUsing(e => e
.Using<JToken>(ctx => ctx.Subject.Should().BeEquivalentTo(ctx.Expectation))
.WhenTypeIs<JToken>()
);
var d = new Dictionary<string, JToken> { ["a"] = 5 };
var e = new Dictionary<string, JToken> { ["a"] = 7 };
d.Should().BeEquivalentTo(e);
} |
Indeed. But I was playing with the idea of having some kind of automatic configuration where adding a reference to the JSON package would automatically add the relevant |
@jnyrup Thanks for the idea, I did not consider that. This will work as a workaround until there might be a better solution :). |
I tried the solution proposed by @jnyrup and it works, unfortunately it kind of breaks the nice display of assertion errors we usually get with Is there any way to solve this problem (by building a more complex |
I think the limitation is in |
If you're okay with using a hacky workaround, which might break at any time. class JTokenEquivalencyStep : EquivalencyStep<JToken>
{
protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
{
string message = null;
using (var assertionScope = new AssertionScope())
{
var subject = comparands.Subject as JToken;
var expectation = comparands.Expectation as JToken;
subject.Should().BeEquivalentTo(expectation, context.Reason.FormattedMessage, context.Reason.Arguments);
if (assertionScope.HasFailures())
{
message = assertionScope.Discard()[0].Replace("JSON document", "{context:JSON document}");
}
}
if (message is not null)
{
AssertionScope.Current.FailWith(message);
}
return EquivalencyResult.AssertionCompleted;
}
} var subject = new { First = (JToken)1, Second = (JToken)2 };
var expectation = new { First = (JToken)3, Second = (JToken)4 };
subject.Should().BeEquivalentTo(expectation,
options => options.Using(new JTokenEquivalencyStep()),
"my {0} message", "failure");
|
Currently when running the following code:
The difference between the values is not detected (i.e. no assertion error is raised). It would be cool if this NuGet package would offer some kind of extension method on EquivalencyAssertionOptions (or similar) to allow me to make the comparison work correctly.
The text was updated successfully, but these errors were encountered: