Almis
Almis

Reputation: 3819

How to compare NaN and NaN is equal in unit test

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

Upvotes: 2

Views: 363

Answers (1)

Orace
Orace

Reputation: 8359

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).

Upvotes: 2

Related Questions