Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fluentassertions/fluentassertions.json

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,387 Commits
1,387 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

build Coverage Status

"With Fluent Assertions, the assertions look beautiful, natural and most importantly, extremely readable" (by Girish)

Available extension methods

  • BeEquivalentTo()
  • ContainSingleElement()
  • ContainSubtree()
  • HaveCount()
  • HaveElement()
  • HaveValue()
  • MatchRegex()
  • NotBeEquivalentTo()
  • NotHaveElement()
  • NotHaveValue()
  • NotMatchRegex()

See "in-code" description for more information.

Usage

Be sure to include using FluentAssertions.Json; otherwise false positives may occur.

using FluentAssertions;
using FluentAssertions.Json;
using Newtonsoft.Json.Linq;

... 
var actual = JToken.Parse(@"{ ""key1"" : ""value"" }");
var expected = JToken.Parse(@"{ ""key2"" : ""value"" }");
actual.Should().BeEquivalentTo(expected);

You can also use IJsonAssertionOptions<> with Should().BeEquivalentTo() assertions, which contains helper methods that you can use to specify the way you want to compare specific data types.

Example:

using FluentAssertions;
using FluentAssertions.Json;
using Newtonsoft.Json.Linq;

... 
var actual = JToken.Parse(@"{ ""value"" : 1.5 }");
var expected = JToken.Parse(@"{ ""value"" : 1.4 }");
actual.Should().BeEquivalentTo(expected, options => options
                .Using<double>(d => d.Subject.Should().BeApproximately(d.Expectation, 0.1))
                .WhenTypeIs<double>());

Also, there is WithoutStrictOrdering() which allows you to compare JSON arrays while ignoring the order of their elements. This is useful when the sequence of items is not important for your test scenario. When applied, assertions like BeEquivalentTo() will succeed as long as the arrays contain the same elements, regardless of their order.

Example:

using FluentAssertions;
using FluentAssertions.Json;
using Newtonsoft.Json.Linq;

...
var actual = JToken.Parse(@"{ ""array"" : [1, 2, 3] }");
var expected = JToken.Parse(@"{ ""array"" : [3, 2, 1] }");
actual.Should().BeEquivalentTo(expected, options => options
                .WithoutStrictOrdering());

About

NewtonSoft.Json extensions for FluentAssertions

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.