Reputation: 198198
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?
Upvotes: 5
Views: 2808
Reputation: 657168
This article explains how to test exceptions https://github.com/dart-lang/test/blob/master/pkgs/test/README.md#asynchronous-tests
test("function hello should throw exception", () {
expect(hello, throwsA(quals("exception of world")));
});
Upvotes: 4
Reputation: 3655
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>()));
});
}
Upvotes: 6