Home > Mobile >  How to compare NaN and NaN is equal in unit test
How to compare NaN and NaN is equal in unit test

Time:12-22

I have a unit test where my result and my expected result have value NaN, but when I do

expect(result, expected);

this returns

Expected: <NaN>
  Actual: <NaN>

I understand this happens for this reason Why is NaN not equal to NaN?

But is there any way to override the equal comparison in order for these two values to be equal? Or maybe some other solution?

I know that I can do result.isNaN but I use a Map for testing that looks like this

{
    'expect': {
        2: 2.0,
        '2.': 2.0,
        '-2.5': -2.5,
        '.5': 0.5,
        '2020-01-06T14:31:00.135Z': double.nan,
        'foo': double.nan,
    }
}

where I iterate through each key and compare the value (expected) with my result, so the result, and expected values above are dynamic that's why I would like to avoid doing special if condition just for checking NaN

CodePudding user response:

You should replace the values in the map by matcher:

2: equals(2),
'foo': isNaN

Then update your logic (looks like it will work as is).

  • Related