Dart Unit Test ThrowsA not working as expected

Dart VM version: 2.0.0 (Fri Aug 3 10:53:23 2018 +0200) on “macos_x64”
MacOSX

// my_class.dart
class MyClass {
  MyClass(String argument): assert(argument != null);
}
// my_class_test.dart
void main() {
  group('constructor', () {
    test('should throw when initialized with null argument', () {
      expect(MyClass(null), throwsA(AssertionError));
    });
  });
}

Expected: Test Passes
Actual:

'my_class.dart': Failed assertion: line 8 pos 16: 'argument != null': is not true.
  dart:core                                                     _AssertionError._throwNew
  my_class.dart 8:16  new MyClass
  unit_tests/my_class_test.dart 7:14           main.<fn>.<fn>

Author: Fantashit

1 thought on “Dart Unit Test ThrowsA not working as expected

  1. you should do something like expect(() => MyClass(null), throwsA(...)) otherwise you throw outside of expect.

Comments are closed.