Skip to Content
TestingUnit Testing

Unit Testing

FakeInjector is a tiny, in-memory injector for pure unit tests. It feeds fakes to the class under test without touching the global container — no Modular.configure, no reset, no leaks between tests.

FakeInjector

Build an injector by chaining add, then resolve with get:

test/order_service_test.dart
import 'package:flutter_test/flutter_test.dart'; import 'package:go_router_modular/testing.dart'; void main() { test('OrderService charges through the gateway', () { final injector = FakeInjector.empty() .add<PaymentGateway>(FakePaymentGateway()) .add<UserRepository>(FakeUserRepository()); final service = OrderService( injector.get<PaymentGateway>(), injector.get<UserRepository>(), ); expect(service.checkout(), isTrue); }); }
  • FakeInjector.empty() — creates an empty injector.
  • add<T>(T instance)immutable: returns a new injector with T registered. The original is unchanged, so the methods chain.
  • get<T>({String? key}) — resolves T.

Missing dependencies fail loudly

If you get<T>() a type you never registered, FakeInjector throws FakeInjectorMissingBindError with a message telling you exactly which type is missing and how to add it — no silent null:

FakeInjector.empty().get<PaymentGateway>(); // throws FakeInjectorMissingBindError: // "nenhum bind registrado para o tipo PaymentGateway."

It is an InjectorReader

FakeInjector implements InjectorReader, so you can pass it anywhere one is expected — for example, to a module’s initState:

final module = CartModule(); module.initState(FakeInjector.empty().add<CartRepository>(FakeCartRepository()));

FakeInjector vs ModularTestScope

FakeInjectorModularTestScope
Use forPure unit test of one classIntegration test of real DI + events
Global stateNoneResets the real container each setUp
SetupNone — just build and passsetUp/tearDown lifecycle
EventsNoYes (records the real bus)

Reach for FakeInjector first when you’re testing a single service or use case. Use ModularTestScope when the test must exercise the real container wiring or the event system.

Next steps

Last updated on