How do you use `BeApproximately` with a value that changes in each iteration of a loop?
See original GitHub issueDescription
- 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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
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.