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?
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>()));
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With