Skip to content

Latest commit

 

History

History
32 lines (17 loc) · 1.12 KB

File metadata and controls

32 lines (17 loc) · 1.12 KB

Testing functions that throw

When writing test expectations for functions that throw, we need a bit careful.

If we call the function directly, the test will fail as it won't catch the exception as we expect.

In these cases, we should always pass a closure when calling the expect method as shown here. 👇


Here's a comparison I made to explain why calling the method directly won't work.

These two implementations are equivalent. And they both fail because the function will execute and throw before expect() is even called. 👇

Hope this will save you some headaches. 👍

Found this useful? Show some love and share the original tweet 🙏

Also published on codewithandrea.com 👇


Previous Next
Why write automated tests? Dart 2.17: Super Initializers