Serialized properties can optionally be ordering alphabetically, ie ignoring the order they are defined when using reflection.
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init() =>
VerifierSettings.SortPropertiesAlphabetically();
}
Dictionaries are ordering by key.
To disable use:
[Fact]
public Task DontOrderDictionaries()
{
var dictionary = new Dictionary<string, string>
{
{
"Entry_1", "1234"
},
{
"Entry_3", "1234"
},
{
"Entry_2", "5678"
}
};
return Verify(dictionary)
.DontSortDictionaries();
}
[Fact]
public Task DontOrderDictionaries()
{
var dictionary = new Dictionary<string, string>
{
{
"Entry_1", "1234"
},
{
"Entry_3", "1234"
},
{
"Entry_2", "5678"
}
};
return Verify(dictionary)
.DontSortDictionaries();
}
Json and JObject are not ordered.
To enable ordering use:
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init() =>
VerifierSettings.SortJsonObjects();
}
Items in an instance of an IEnumerable can be ordered.
This is helpful when verifying items that can have an inconsistent order, for example reading items from a database.
[ModuleInitializer]
public static void OrderEnumerableByInitializer() =>
VerifierSettings.OrderEnumerableBy<TargetForGlobal>(_ => _.Value);
[Fact]
public Task EnumerableOrder()
{
var settings = new VerifySettings();
settings.OrderEnumerableBy<Target>(_ => _.Value);
return Verify(
new List<Target>
{
new("a"),
new("c"),
new("b")
},
settings);
}
[Fact]
public Task EnumerableOrderFluent() =>
Verify(
new List<Target>
{
new("a"),
new("c"),
new("b")
})
.OrderEnumerableBy<Target>(_ => _.Value);
The resulting file will be:
[
{
Value: a
},
{
Value: b
},
{
Value: c
}
]
[ModuleInitializer]
public static void OrderEnumerableByDescendingInitializer() =>
VerifierSettings.OrderEnumerableByDescending<TargetForGlobalDescending>(_ => _.Value);
[Fact]
public Task OrderEnumerableByDescending()
{
var settings = new VerifySettings();
settings.OrderEnumerableByDescending<Target>(_ => _.Value);
return Verify(
new List<Target>
{
new("a"),
new("c"),
new("b")
},
settings);
}
[Fact]
public Task OrderEnumerableByDescendingFluent() =>
Verify(
new List<Target>
{
new("a"),
new("c"),
new("b")
})
.OrderEnumerableByDescending<Target>(_ => _.Value);
The resulting file will be:
[
{
Value: c
},
{
Value: b
},
{
Value: a
}
]