Coder Social home page Coder Social logo

fluentassertionsex's Introduction

Fluent Assertions Extensions

This repo contains extensions for Fluent Assertions.

Currently solely for System.Text.Json.

Installation

Currently not available.

System.Text.Json

There are basic assertions for:

  • JsonElement and
  • JsonProperty

JsonElement

There are the following assertions for JSON elements:

  • HaveProperty, to verify whether the element contains a property with a specific name, provides chaining the property assertions
  • HaveValueKind, to verify the value kind
  • HaveEqualValue, to verify the value and
  • BeAString, to verify whether the value is of kind String, provides chaining the property assertions

JsonElement.Should().HaveProperty(string)

Asserts whether the element has got a property name fullName.

using var documentBase = JsonDocument.Parse(@" { ""fullName"": ""Bobby"" }");

documentBase.RootElement.Should().HaveProperty("fullName");

Asserts whether the element has got a property name fullName and its value is the string Bobby.

using var documentBase = JsonDocument.Parse(@" { ""fullName"": ""Bobby"" }");

documentBase.RootElement
    .Should().HaveProperty("fullName")
    .Which.SubjectValue.Should().BeAString()
    .Subject.Should().Be("Bobby");

JsonElement.Should().HaveValueKind(JsonValueKind)

Asserts whether the value kind of the element is the expected one.

using var documentBase = JsonDocument.Parse(@" { ""fullName"": ""Bobby"" }");
using var documentBase = JsonDocument.Parse(@" { ""firstName"": ""Bobby"" }");

documentBase.RootElement.Should().HaveValueKind(JsonValueKind.Object);

JsonElement.Should().HaveEqualValue(JsonElement)

Asserts whether the element is equal to the expected element.

Simple equal objects

using var documentBase = JsonDocument.Parse(@" { ""firstName"": ""Bobby"", ""secondName"":""Foo"" }");
using var expected = JsonDocument.Parse(@" {  ""secondName"":""Foo"", ""firstName"": ""Bobby"" }");

var actualProperty = documentBase.RootElement;
var expectedProperty = expected.RootElement;

actualProperty.Should().HaveEqualValue(expectedProperty);

Simple unequal objects

using var documentBase = JsonDocument.Parse(@" { ""firstName"": ""Bobby"", ""secondName"":""Foo"" }");
using var expected = JsonDocument.Parse(@" {  ""secondName"":""Bar"", ""firstName"": ""Bobby"" }");

var actualProperty = documentBase.RootElement;
var expectedProperty = expected.RootElement;

Action act = () => actualProperty.Should().HaveEqualValue(expectedProperty);

act.Should().Throw<XunitException>();

JsonElement.Should().BeAString()

Asserts whether the value of the element is of kind String.

JsonProperty

There are two assertions for JSON properties:

  • Be, to verify the whole property with the expected property and
  • BeOfValueKind, to verify the value kind

JsonProperty.Should().Be(JsonProperty)

using var documentBase = JsonDocument.Parse(@" { ""firstName"": ""Bobby"" }");
using var expected = JsonDocument.Parse(@" { ""firstName"": ""Bobby"" }");

var property = documentBase.RootElement.GetTypedProperty("firstName");
var expectedProperty = expected.RootElement.GetTypedProperty("firstName");

property.Should().Be(expectedProperty);

JsonProperty.Should().BeOfValueKind(JsonProperty)

using var documentBase = JsonDocument.Parse(@" { ""firstName"": ""Bobby"" }");

var property = documentBase.RootElement.GetTypedProperty("firstName");

property.Should().BeOfValueKind(JsonValueKind.String);

fluentassertionsex's People

Contributors

manne avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.