Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a function which throws exception, in Dart?

Say I have a function which throws exception:

hello() {
    throw "exception of world";
}

I want to test it, so I write a test:

test("function hello should throw exception", () {
    expect(()=>hello(), throwsA("exception of world"));
});

You can see I didn't call hello() directly, instead, I use ()=>hello().

It works but I wonder if is there any other way to write tests for it?

like image 979
Freewind Avatar asked Mar 20 '23 16:03

Freewind


1 Answers

You can pass hello directly by name instead of creating a closure that only calls hello.

This unit-test passes:

main() {
  test("function hello should throw exception", () {
      expect(hello, throwsA(new isInstanceOf<String>()));
  });
}
like image 166
Ganymede Avatar answered Apr 02 '23 08:04

Ganymede