Assert.AreEquivalent Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
| AreEquivalent<T>(T, T, Boolean, String, String, String) |
Tests whether two object graphs are structurally equivalent (deep equality) and throws an exception if they are not. |
| AreEquivalent<T>(T, T, String, String, String) |
Tests whether two object graphs are structurally equivalent (deep equality) and throws an exception if they are not. |
AreEquivalent<T>(T, T, Boolean, String, String, String)
- Source:
- Assert.AreEquivalent.cs
Tests whether two object graphs are structurally equivalent (deep equality) and throws an exception if they are not.
public static void AreEquivalent<T>(T? expected, T? actual, bool strict, string? message = "", string expectedExpression = "", string actualExpression = "");
static member AreEquivalent : 'T * 'T * bool * string * string * string -> unit
Public Shared Sub AreEquivalent(Of T) (expected As T, actual As T, strict As Boolean, Optional message As String = "", Optional expectedExpression As String = "", Optional actualExpression As String = "")
Type Parameters
- T
The type of values to compare.
Parameters
- expected
- T
The first value to compare. This is the value the test expects.
- actual
- T
The second value to compare. This is the value produced by the code under test.
- strict
- Boolean
When true, the comparison fails if actual declares any
public properties or fields that are not present on expected's runtime type,
or if any extra dictionary keys are present on actual. When false
(the default), additional members and dictionary keys on actual are ignored.
When the runtime types of expected and actual are identical,
this flag has no effect on the public-member comparison, but it still rejects extra dictionary keys
on actual when comparing dictionaries.
- message
- String
The message to include in the exception when actual
is not equivalent to expected. The message is shown in
test results.
- expectedExpression
- String
The syntactic expression of expected as given by the compiler via caller argument expression. Users shouldn't pass a value for this parameter.
- actualExpression
- String
The syntactic expression of actual as given by the compiler via caller argument expression. Users shouldn't pass a value for this parameter.
Exceptions
Thrown if expected and actual are not structurally equivalent.
Applies to
AreEquivalent<T>(T, T, String, String, String)
- Source:
- Assert.AreEquivalent.cs
Tests whether two object graphs are structurally equivalent (deep equality) and throws an exception if they are not.
public static void AreEquivalent<T>(T? expected, T? actual, string? message = "", string expectedExpression = "", string actualExpression = "");
static member AreEquivalent : 'T * 'T * string * string * string -> unit
Public Shared Sub AreEquivalent(Of T) (expected As T, actual As T, Optional message As String = "", Optional expectedExpression As String = "", Optional actualExpression As String = "")
Type Parameters
- T
The type of values to compare.
Parameters
- expected
- T
The first value to compare. This is the value the test expects.
- actual
- T
The second value to compare. This is the value produced by the code under test.
- message
- String
The message to include in the exception when actual
is not equivalent to expected. The message is shown in
test results.
- expectedExpression
- String
The syntactic expression of expected as given by the compiler via caller argument expression. Users shouldn't pass a value for this parameter.
- actualExpression
- String
The syntactic expression of actual as given by the compiler via caller argument expression. Users shouldn't pass a value for this parameter.
Exceptions
Thrown if expected and actual are not structurally equivalent.
Remarks
This assertion performs a deep, order-sensitive structural comparison of two object graphs. It differs from AreEquivalent(ICollection, ICollection), which checks only top-level collection equivalence without regard to element order.
The comparison rules are:
- Reference-equal values (including both
null) are considered equivalent. - Primitive-like types (numerics, Boolean, Char, String, enums, DateTime, DateTimeOffset, TimeSpan, Guid, Uri, Type, Version) are compared with Equals(Object). On target frameworks that ship them,
DateOnly,TimeOnly,Half,Int128, andUInt128are also treated as primitive-like. - If the static (compile-time) type of a value implements IEquatable<T> for itself, Equals(T) is used and recursion stops at that point. Plain Equals(Object) overrides are ignored — the comparer always recurses into members for those.
- Dictionaries (IDictionary, IDictionary<TKey, TValue>, or IReadOnlyDictionary<TKey,TValue>) are compared by key set, with values compared recursively. Lookups are routed through the source dictionary so its IEqualityComparer<T> for keys is preserved.
- Other enumerables (excluding String) are compared element-by-element in the iteration order. Comparison is order-sensitive; if order is irrelevant, use AreEquivalent(ICollection, ICollection) on the collection itself.
- Other reference types are compared by recursing into all public instance properties (with a public getter and no index parameters) and public instance fields. Static, non-public, and indexed members are ignored.
- Reference cycles are tracked and traversed at most once per (expected, actual) pair. The same mechanism enforces graph topology: if the same reference on one side is paired with different references on the other side, the assertion fails.
- If a user-defined Equals(T), member getter, or dictionary access (
TryGetValue,ContainsKey, or enumeration) throws while being evaluated, the assertion fails with a message describing the exception type and message. - Comparison stops after a bounded recursion depth to avoid StackOverflowException on unusually deep object graphs, and reports that condition as an assertion failure.