tezvyn:

Unit test a BLoC handling an AddItem event and emitting state

AI-drafted, machine-checkedSource: pub.devintermediate
WHAT IT TESTS

Whether you know BLoC testing idioms and why bloc_test beats manual assertions.

ANSWER OUTLINE

Use blocTest with build, act, expect; mention seed, skip, and mocktail for dependencies.

WHAT THIS TESTS: This question checks whether you understand how to verify BLoC behavior through its public API of events and states rather than its internal implementation. The interviewer wants to see that you know how to set up a controlled environment, trigger state changes via events, and assert on ordered emissions without fragile stream boilerplate. It also surfaces whether you know the ecosystem tooling that makes this repeatable.

A GOOD ANSWER COVERS: First, import bloc_test and instantiate the BLoC inside the build callback so each test gets a fresh instance. Second, use the act callback to add the AddItem event to the bloc under test. Third, use the expect callback to return a list of state matchers that must be emitted in exact order after act runs. Fourth, mention that bloc_test automatically closes the stream and verifies no extra states were emitted, which removes the need for manual subscriptions and completers. Fifth, note that mocktail works with bloc_test to stub repositories or data sources injected into the BLoC, keeping the test isolated to state-machine logic.

COMMON WRONG ANSWERS: A red flag is manually creating a StreamController, listening to bloc.stream, and collecting states in a list for manual comparison; this is error-prone and omits the automatic close check. Another mistake is calling emit or onChange directly from the test instead of driving the bloc through add, because that bypasses the event handler and event transformer logic. A third wrong pattern is forgetting to seed the initial state and then asserting on it, which causes off-by-one failures when the first emission is actually the unchanged seed state.

LIKELY FOLLOW-UPS: The interviewer may ask how you test async side effects like debounce or throttle, where you would use the wait parameter in blocTest with a Duration. They might also ask how to verify that a repository method was called exactly once, which is done in the verify callback of blocTest. Another follow-up is testing error paths by using the errors parameter to assert that a specific exception is thrown, or asking how you would test a Cubit instead of a BLoC, which uses the same blocTest helper but typically omits the event layer.

ONE CONCRETE EXAMPLE: Suppose a ShoppingBloc starts with an empty list state. Using blocTest, you set build to return ShoppingBloc with a mocked repository, act to add AddItem('Milk'), and expect to return a matcher list containing a state whose items list equals ['Milk']. If the BLoC debounces rapid AddItem events, you would add wait: const Duration(milliseconds: 300) so blocTest pauses before evaluating expect. You could also use seed: () => ShoppingState(items: ['Bread']) to start mid-session and assert the new state appends 'Milk' to the existing list.

Source: bloclibrary.dev / pub.dev bloc_test 10.0.0

Read the original → pub.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.