Trying to learn FluentAssertions, but have a weird result from Should().Be(new Wotsit ())

I'm trying to learn TTD and I wrote a test to compare pseudo-tuple classes after multiplication. The results seem identical, but it counts as a failure. What did I do wrong? My C# ability is decent, but I'm new to TTD and FluentAssertions
No description
No description
7 Replies
TheRainInSpainFalls
TheRainInSpainFallsOP10mo ago
hmm... it seems to be an issue with me initialising the variable inside should be. result.Should().BeEquivelentTo(expected); works Urgh! Expected property result.Blue to be 0.04, but found 0.04000000000000001. I really should just use three seperate result.Blue.Should().BeApproximately(0.04, tolerence); I know doubles are slightly inprecise compared to decimals
dreadfullydistinct
You can tell fluentassertions to use different behaviour for doubled
orderDto.Should().BeEquivalentTo(order, options => options
.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1.Seconds()))
.WhenTypeIs<DateTime>());
orderDto.Should().BeEquivalentTo(order, options => options
.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1.Seconds()))
.WhenTypeIs<DateTime>());
TheRainInSpainFalls
TheRainInSpainFallsOP10mo ago
Thank you, I'll check it when I wake up.
dreadfullydistinct
And you could also apply that globally using the static AssertionOptions But I can’t quite remember the syntax for that
TheRainInSpainFalls
TheRainInSpainFallsOP10mo ago
That'll be worth trying, non-decimals/ints rarely match perfectly.
dreadfullydistinct
Yeah, I mainly use it with time, when I’m asserting against the creation date of something returned from my api. Obviously it is approximately DateTime.UtcNow but +- how long the request took So I allow 1 second of tolerance
Want results from more Discord servers?
Add your server