Class JsonFailureMessage
- Namespace
- JsonAssertions
- Assembly
- JsonAssertions.TUnit.dll
Renders failure-message text for the JSON assertions. The boolean / resolution primitives in JsonPath and JsonValueComparison remain authoritative for pass / fail; these helpers produce the human-readable explanation surfaced in the assertion exception when an assertion fails.
public static class JsonFailureMessage
- Inheritance
-
JsonFailureMessage
- Inherited Members
Remarks
The point of the package over a hand-rolled TryGetProperty(...).IsTrue()
helper is that a failure says where on the path resolution stopped. Every
path-resolution failure renders a two-line block: resolved as far as: (the longest
prefix that resolved) and a reason line distinguishing "the object has no such property"
from "the path tried to read a property off a non-object".
Lines are terminated with the literal LF byte so the rendered text is byte-stable across platforms. Numeric formatting uses InvariantCulture.
Public surface (extension point for consumer-authored typed JSON assertions):
the four path-family factories (PropertyNotFound(string, JsonPathResolution),
ValueMismatch(string, JsonPathResolution, string),
ShapeMismatch(string, JsonPathResolution, string), and
ParseFailure(JsonException)) are part of the v0.3.0+ stable public API.
Consumers writing their own typed JSON assertions can compose these factories with their
custom [GenerateAssertion] methods to produce failure messages that match the
package's diagnostic style. Family-pattern consistency: mirrors
MathAssertions.MathFailureMessage.
Internal surface: HTTP-response, ProblemDetails, and roundtrip-specific factories stay internal. Those failure modes are bound to the specific assertions that produce them (no extension value); keeping them internal preserves future-flexibility on the message text for context-specific failures.
The exact rendered text of the public factories may evolve in minor versions (added punctuation, reworded reason lines); consumers should pin tests against the outcome (pass / fail) and key tokens (e.g. the path), not against full message equality.
Methods
ContainsMismatch(IReadOnlyList<JsonDifference>)
Renders the failure for a subset (contains) assertion: every path where the actual document does not contain the expected value, each with the category of difference and a rendered view of both sides. Unlike EquivalenceMismatch(JsonDifference), all differences are listed in one message (subset assertions replace a block of per-field checks, so a single run must enumerate every field that is wrong).
public static string ContainsMismatch(IReadOnlyList<JsonDifference> differences)
Parameters
differencesIReadOnlyList<JsonDifference>The differences found; must be non-empty.
Returns
- string
The rendered multi-line failure body.
Exceptions
- ArgumentNullException
differencesis null.
EquivalenceMismatch(JsonDifference)
Renders the failure for a structural-equivalence assertion: the path where the two documents diverge, the category of difference, and a rendered view of each side. Container values are shown as truncated raw JSON so an object/array mismatch reports its content rather than just its kind.
public static string EquivalenceMismatch(JsonDifference difference)
Parameters
differenceJsonDifference
Returns
ExpectedParseFailure(JsonException)
Renders the failure when the expected JSON supplied to an equivalence assertion is itself malformed (a test-authoring error rather than a value mismatch).
public static string ExpectedParseFailure(JsonException exception)
Parameters
exceptionJsonException
Returns
ParseFailure(JsonException)
Renders the failure for a JSON string overload whose input could not be parsed at all. A malformed response body is a legitimate runtime scenario, so it is surfaced as an explained assertion failure rather than an escaping JsonException.
public static string ParseFailure(JsonException exception)
Parameters
exceptionJsonException
Returns
PropertyNotFound(string, JsonPathResolution)
Renders the failure for a positive property-existence assertion: the path did not resolve, and the message says how far it got and why it stopped.
public static string PropertyNotFound(string path, JsonPathResolution resolution)
Parameters
pathstringresolutionJsonPathResolution
Returns
PropertyShouldNotExist(string, JsonPathResolution)
Renders the failure for a negative property-existence assertion: the path resolved when it was asserted not to.
public static string PropertyShouldNotExist(string path, JsonPathResolution resolution)
Parameters
pathstringresolutionJsonPathResolution
Returns
ShapeMismatch(string, JsonPathResolution, string)
Renders the failure for a shape assertion (array length, non-empty / empty array, value kind). When the path did not resolve, the failure-point block is appended; when it resolved but the element has the wrong shape, the found shape is shown.
public static string ShapeMismatch(string path, JsonPathResolution resolution, string expectedDescription)
Parameters
pathstringresolutionJsonPathResolutionexpectedDescriptionstring
Returns
ValueMismatch(string, JsonPathResolution, string)
Renders the failure for a value-at-path assertion. When the path itself did not resolve, the failure-point block is appended; when it resolved but the value did not match, the found value is shown against the expected one.
public static string ValueMismatch(string path, JsonPathResolution resolution, string expectedDescription)
Parameters
pathstringresolutionJsonPathResolutionexpectedDescriptionstring