C
Join ServerC#
help
❔ FluentAssertions excluding collection elements
Ccamel12/8/2022
I have two IEnumerable<Claim> tmp1, tmp2 and I want to see if they are equal, except for the claims "exp", "nbf", "iat" because those are timestamps and dependent on the execution time. How can I solve this without having to mock every single thing that gives a time?
tmp1.Should().BeEquivalentTo(tmp2, options =>
{
options.Excluding(x => x.Type == JwtRegisteredClaimNames.Exp);
options.Excluding(x => x.Type == JwtRegisteredClaimNames.Iat);
options.Excluding(x => x.Type == JwtRegisteredClaimNames.Nbf);
return options;
});
Zzonedetec12/8/2022
maybe use linq then?
var claimsToExclude = new List<string>
{
JwtRegisteredClaimNames.Exp,
JwtRegisteredClaimNames.Iat,
JwtRegisteredClaimNames.Nbf
};
var tmp1Filtered = tmp1.Where(x => !claimsToExclude.Contains(x.Type));
var tmp2Filtered = tmp2.Where(x => !claimsToExclude.Contains(x.Type));
tmp1Filtered.Should().BeEquivalentTo(tmp2Filtered);
Zzonedetec12/8/2022
I don't think there's another way of doing what you want to do using BeEquivalentTo.
Zzonedetec12/8/2022
or maybe you can try this too:
var claimsToCompare = tmp1.Except(new[]
{
new Claim(JwtRegisteredClaimNames.Exp, ""),
new Claim(JwtRegisteredClaimNames.Iat, ""),
new Claim(JwtRegisteredClaimNames.Nbf, "")
});
claimsToCompare.Should().BeEquivalentTo(tmp2, options =>
{
return options;
});
Ccamel12/8/2022
alright that will do
Ccamel12/8/2022
thanks!
AAccord12/9/2022
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.