Skip to content

Conversation

@martincostello
Copy link
Contributor

@martincostello martincostello commented Jan 20, 2025

Fix assertions incorrectly failing when a tolerance of "zero" for the type is used.

I migrated Polly from FluentAssertions to Shouldly over the weekend and we had a test like this:

value.Should().BeCloseTo(expected, TimeSpan.Zero); // value is a TimeSpan

Migrating to Shouldly, I changed this to:

value.ShouldBe(expected, TimeSpan.Zero);

The test then failed despite the values being equal.

I guess the original author wanted to be explicit? This was easily resolved by just changing the assertion to value.ShouldBe(expected).

This PR fixes that behaviour by changing < to <= for TimeSpan, as well as decimal, double, DateTime and DateTimeOffset that also have the same issue.

If non-zero tolerances being equal shouldn't match, then an alternative fix could be to special-case "zero" for these cases in Is.

Fix assertions incorrectly failing when a tolerance of zero is used.
@slang25
Copy link
Member

slang25 commented Jan 20, 2025

Thanks @martincostello, the tolerance should be inclusive IMO, so this is bug, thanks for the fix

@slang25 slang25 merged commit 4d92bf9 into shouldly:master Jan 20, 2025
3 checks passed
@martincostello martincostello deleted the fix-zero-tolerance-false-failures branch January 20, 2025 16:06
@slang25 slang25 added this to the 4.3.0 milestone Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants