Dart's `expect` and Matchers: Writing Better Tests
Dart's expect and Matchers are a grammar for your tests, letting you define complex rules beyond simple equality. Use them to verify values, check for exceptions, and test async Futures/Streams.
Why it exists
Simple assertions like assert(a == b) are too limited for robust testing. Modern applications have complex states, error conditions, and asynchronous data flows. The matcher library was created to provide an expressive, readable, and extensible way to specify test expectations, especially for these complex cases.
The mental model
Think of expect(actual, matcher) as a declarative statement: "I expect this actual value to satisfy the rule defined by this matcher." Matchers are pre-built, composable rules that describe the expected outcome. Instead of writing procedural code to check a value, you declare the desired state, making tests easier to read and write.
How it works
The expect() function takes two arguments: the actual value produced by your code, and the matcher. If you pass a plain value as the second argument, it's implicitly wrapped in an equals() matcher. The function runs the matcher's logic against the actual value. If it passes, the test continues. If it fails, it throws a TestFailure with a descriptive message. For asynchronous code, special matchers like completion() intelligently wait for the operation to finish before checking the result, preventing flaky tests.
When to use it
Matchers are the standard for assertions in Dart and Flutter tests. Use them for three main scenarios: first, for simple value validation using matchers like equals(), contains(), or inInclusiveRange(); second, for verifying that code throws a specific error using throwsA() or throwsFormatException; and third, for testing asynchronous code, using completion() for Futures and emitsInOrder() for Streams.
When not to use it
Matchers and expect are strictly for testing environments. Never use them in your production application code for control flow or error handling. expect() is designed to halt a test and report a failure, which is not desirable behavior in a live app. Use standard try-catch blocks, conditional logic, and state management for handling logic and errors in your app.
One canonical example
Testing a Future requires the completion() matcher to ensure the test waits for the Future to resolve. This example tests that a Future completes with the value 10.
expect(Future.value(10), completion(equals(10)));Without completion(), the test would incorrectly compare the Future object itself to the number 10 and fail immediately.
Interview question
Why should Dart's expect and Matchers not be used in production application code?
- a.They introduce security vulnerabilities when included in a deployed application.
- b.They can only process static data and cannot interact with dynamic user inputs.
- c.They halt execution on failure, which is unsuitable for live application error handling.Correct
- d.They are primarily designed for performance benchmarking, not functional validation.
Why? this is the answer
The card explicitly states that expect() is designed to halt a test and report a failure, which is not desirable behavior in a live app. It does not mention performance benchmarking or security vulnerabilities as reasons for avoiding their use in production.
Just read this? Test yourself on what you have been reading.
Read the original → pub.dev
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.
See open roles