question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you use `BeApproximately` with a value that changes in each iteration of a loop?

See original GitHub issue

Description

  • I’m iterating over 2 lists
  • I want to compare the 2 values at each point in the list
  • I want the value to be within 0.1% of each other

Complete minimal example reproducing the issue

var sheetRows = new List<float> { -24.0, 0.1, 10};
var loads= new List<float> { -24.0, 0.1, 10.05};
foreach ((var xl, var db) in sheetRows.Zip(loads, (n, w) => (n, w)))
{
   xl.Should().BeApproximately(db, db*0.001f);
}

Expected behavior:

To pass

Actual behavior:

Expected xl to approximate -24F +/- -0.024F, but -24F differed by 0F.
   at FluentAssertions.Execution.LateBoundTestFramework.Throw(String message)
   at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
   at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message)
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
   at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc)
   at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args)
   at FluentAssertions.NumericAssertionsExtensions.FailIfDifferenceOutsidePrecision[T](Boolean differenceWithinPrecision, NumericAssertions`1 parent, T expectedValue, T precision, T actualDifference, String because, Object[] becauseArgs)
   at FluentAssertions.NumericAssertionsExtensions.BeApproximately(NumericAssertions`1 parent, Single expectedValue, Single precision, String because, Object[] becauseArgs)
   at RoutingTests.UnitResponseModuleTests.U010_CheckTranslationAsync(Int64 sId, String sheetName, Int32 expected, Int32 excelRowCount) in C:\Users\sogscx\source\repos\Float\Compute\Test\RoutingTests\UnitResponseModuleTests.cs:line 480
   at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.BlockUntilCompleted()

Versions

  • Which version of Fluent Assertions are you using? latest
  • Which .NET runtime and version are you targeting? E.g. .NET framework 4.6.1 or .NET Core 2.1. .NET Core 3.1 running NUnit

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jnyrupcommented, Sep 15, 2020

@Smurf-IV The fix will be available in the upcoming 6.0 release.

I’ve added guard against negative values, but not zeroes. Approximating a value with a zero precision would of course be more clearly written as an exact comparison, but it works as expected.

0reactions
Smurf-IVcommented, Sep 14, 2020

I cannot re-open, but as per SO. There needs to be argument validation for the precision so that -ves (Or zero) are not used for the tolerance value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How do you use `BeApproximately` with a value that ...
I'm iterating over 2 lists; I want to compare the 2 values at each point in the list; I want the value to...
Read more >
3.1-3.10 AP-Style MC Practice & Quizzes Flashcards
The repeat loop checks the value of the item at position index# on Grades. If it is smaller than 80 then it is...
Read more >
Assignment 10: The Indefinite Loop — CS108, Boston University
Inside the loop, you will use the response about the previous guess to adjust the search space. On each iteration, the program will...
Read more >
CS Labs: Lab 6
We want you to use certain constraints while assigning the values. On each iteration of the loop: r, g, and b must be...
Read more >
Chapter 15 Writing for-loops and functions
A for loop is a set of lines of code which we wish to repeat for a vector of values and where we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found