Im currently trying to build widget tests for a project (focusing on TDD). I'm using Injectable to generate the dependency injection as well as the environments to swap between various implementations. The problem I'm having is that I'm not able to use the @GenerateMocks([IAuthRepository])
like in my unit tests and so need to inject the mock into GetIt.
@Environment("test")
@LazySingleton(as: IAuthRepository)
class MockFirebaseAuthFascade with Mock implements IAuthRepository {}
In my widget test I first inject with the text env and then use getIt<IAuthRepository>()
to call the mocked instance so I can use when
and verify
in my test:
void main() {
setUpAll(() async {
configureInjection(Environment.test);
});
testWidgets(
'GIVEN form is initial state '
'WHEN ElevatedButton is pressed '
'THEN see two error messaged below TextField', (tester) async {
// Arrange
await tester.pumpWidget(
MaterialApp(
localizationsDelegates: const [
Localize.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: Localize.delegate.supportedLocales,
home: BlocProvider(
create: (context) => getIt<SignInFormBloc>(),
child: const Scaffold(
body: SignInForm(),
),
),
),
);
final mockAuth = getIt<IAuthRepository>();
final Finder signIn =
find.byKey(const Key("auth_sign_in_with_email_button"));
// Act
await tester.pumpAndSettle();
await tester.tap(signIn);
await tester.pump(const Duration(seconds: 1));
// Assert
verifyNever(mockAuth.resetPassword(email: anyNamed("named")));
});
}
What I'm getting is an error on the anyNamed("named")
as its only mocked with, not using @GenerateMocks([IAuthRepository])
like in my unit tests. What I don't understand, or can find the answer, is how am I supposed to use the @GenerateMocks
with Injectable? Or is there another way to generate better mocks for my testing env?
Any guidance or suggestions is much appreacted.