Dario Digregorio
Dario Digregorio

Reputation: 452

Dart Test using type check on Exception is returning wrong result

I have this test that does fail when run. I am not sure why though since the a should be true. Can somebody explain me why?

  test('test error', () async {
    const error = Exception;
    const a = error is Exception;
    expect(a, true);
  });

Upvotes: 0

Views: 179

Answers (1)

jamesdlin
jamesdlin

Reputation: 90025

error is not an instance of an Exception. error is the Exception type, and x is T checks if x is an instance of T. Therefore:

  • Exception() is Exception would be true.
  • error is Exception (equivalent to Exception is Exception) would be false.
  • error is Type (equivalent to Exception is Exception) would be true.
  • error == Exception (equivalent to Exception == Exception) would be true.

Upvotes: 0

Related Questions