I am trying to test how is my widget reacting to BloC state changes.
This is how my test look like:
class MockAuthBloc extends Mock implements AuthBloc {}
void main() {
initHydratedStorage();
FlavorConfig(
flavor: Flavor.MOCK,
baseUrl: ''
);
late MockAuthBloc authBloc;
late User user;
setUp(() {
authBloc = MockAuthBloc();
user = User(
email: '[email protected]',
name: 'mock',
hasCompletedProfile: true,
language: 'pl',
id: 1
);
});
testWidgets('navigates to MainNavigator when authenticated', (tester) async {
when(() => authBloc.state).thenReturn(AuthState.authenticated(user));
await tester.pumpWidget(
MaterialApp(
home: BlocProvider<AuthBloc>.value(
value: authBloc,
child: const LoginPage(),
),
),
);
await tester.pumpAndSettle();
expect(find.byType(MainNavigator), findsOneWidget);
});
}
but when(() => authBloc.state).thenReturn(AuthState.authenticated(user));
is giving me error The argument type 'AuthState' can't be assigned to the parameter type 'AuthState Function()'.
and I do not know how to handle it properly.
Here is how my AuthState class looking:
part of 'auth_bloc.dart';
enum AuthStatus { unknown, authenticated, guest, failure, semiAuthenticated}
class AuthState {
final AuthStatus status;
final bool isFirstEntry;
final bool isLoadingLong;
final String? errorMessage;
final String? email;
final User? user;
const AuthState({
this.status = AuthStatus.unknown,
this.isFirstEntry = true,
this.isLoadingLong = false,
this.errorMessage,
this.email,
this.user
});
const AuthState.unknown() : this();
const AuthState.authenticated(User user)
: this(
status: AuthStatus.authenticated,
user: user,
isFirstEntry: false,
isLoadingLong: false
);
Maybe I am testing it completely wrong, but I am new to testing, I am trying to learn it, but I have some problem with mocking.
EDIT:
I tried different approaches and get different errors for them:
when(() => authBloc.state).thenReturn(() => AuthState(
status: AuthStatus.authenticated,
isFirstEntry: false,
isLoadingLong: false,
user: user
));
gives me Bad state: No method stub was called from within `when()`. Was a real method called, or perhaps an extension method?
when(authBloc.state).thenReturn(AuthState(
status: AuthStatus.authenticated,
isFirstEntry: false,
isLoadingLong: false,
user: user
));
gives me type 'Null' is not a subtype of type 'AuthState'
Looks like nothing is working for me and it is driving me crazy
I am trying to test how is my widget reacting to BloC state changes.
This is how my test look like:
class MockAuthBloc extends Mock implements AuthBloc {}
void main() {
initHydratedStorage();
FlavorConfig(
flavor: Flavor.MOCK,
baseUrl: 'https://mock.api'
);
late MockAuthBloc authBloc;
late User user;
setUp(() {
authBloc = MockAuthBloc();
user = User(
email: '[email protected]',
name: 'mock',
hasCompletedProfile: true,
language: 'pl',
id: 1
);
});
testWidgets('navigates to MainNavigator when authenticated', (tester) async {
when(() => authBloc.state).thenReturn(AuthState.authenticated(user));
await tester.pumpWidget(
MaterialApp(
home: BlocProvider<AuthBloc>.value(
value: authBloc,
child: const LoginPage(),
),
),
);
await tester.pumpAndSettle();
expect(find.byType(MainNavigator), findsOneWidget);
});
}
but when(() => authBloc.state).thenReturn(AuthState.authenticated(user));
is giving me error The argument type 'AuthState' can't be assigned to the parameter type 'AuthState Function()'.
and I do not know how to handle it properly.
Here is how my AuthState class looking:
part of 'auth_bloc.dart';
enum AuthStatus { unknown, authenticated, guest, failure, semiAuthenticated}
class AuthState {
final AuthStatus status;
final bool isFirstEntry;
final bool isLoadingLong;
final String? errorMessage;
final String? email;
final User? user;
const AuthState({
this.status = AuthStatus.unknown,
this.isFirstEntry = true,
this.isLoadingLong = false,
this.errorMessage,
this.email,
this.user
});
const AuthState.unknown() : this();
const AuthState.authenticated(User user)
: this(
status: AuthStatus.authenticated,
user: user,
isFirstEntry: false,
isLoadingLong: false
);
Maybe I am testing it completely wrong, but I am new to testing, I am trying to learn it, but I have some problem with mocking.
EDIT:
I tried different approaches and get different errors for them:
when(() => authBloc.state).thenReturn(() => AuthState(
status: AuthStatus.authenticated,
isFirstEntry: false,
isLoadingLong: false,
user: user
));
gives me Bad state: No method stub was called from within `when()`. Was a real method called, or perhaps an extension method?
when(authBloc.state).thenReturn(AuthState(
status: AuthStatus.authenticated,
isFirstEntry: false,
isLoadingLong: false,
user: user
));
gives me type 'Null' is not a subtype of type 'AuthState'
Looks like nothing is working for me and it is driving me crazy
Share Improve this question edited Feb 7 at 7:52 Karol Wiśniewski asked Feb 6 at 14:02 Karol WiśniewskiKarol Wiśniewski 5081 gold badge9 silver badges24 bronze badges 2 |1 Answer
Reset to default 0The documentation I mentioned in the comment manually override a method with a non-nullable return type suggests creating a stub for the getter returning a non-nullable type.
A minimal test file would look like this:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
class AuthBloc {
String state = 'loading';
}
class MockAuthBloc extends Mock implements AuthBloc {
// Stub for a getter returning a non-null type
@override
String get state => super.noSuchMethod(
Invocation.getter(#state),
returnValue: 'Mockito: String from stub: state',
);
}
void main() {
testWidgets('AuthStatus status test', (WidgetTester tester) async {
final authBloc = MockAuthBloc();
when(authBloc.state).thenReturn('authenticated');
expect(authBloc.state, 'authenticated');
});
}
The test above completes with:
$ flutter test
00:04 +1: All tests passed!
() => authBloc.state
, whenauthBloc.state
was expected. This might be relevant: How to manually override a method with a non-nullable return type. – Dan R Commented Feb 6 at 17:10when
statement, I tried different things, but I still get different errors fromBad state: No method stub was called from within `when()`. Was a real method called, or perhaps an extension method?
totype 'Null' is not a subtype of type 'AuthState'
I really don't know how I should write this to mock my state correctly ``` I edited my question with few other approaches – Karol Wiśniewski Commented Feb 7 at 7:49