i try to write tests for my code where i am using BLoC as state management and inside bloc i created variable states using buil_value, which is
import 'package:built_value/built_value.dart';
import 'package:equatable/equatable.dart';
import '../../domain/entity/user_entity.dart';
part 'login_state.g.dart';
abstract class LoginState implements Built<LoginState,LoginStateBuilder> {
bool? get isSuccess;
bool? get isLoading;
String? get error;
UserEntity? get user;
bool? get isOffline;
LoginState._();
factory LoginState([void Function(LoginStateBuilder) updates]) = _$LoginState;
factory LoginState.initial() {
return LoginState((b) => b
..isLoading = false
..isSuccess = false
..isOffline = false
..error = ''
..user = UserEntity()
);
}}
i am using clean code architecture where data layer, domain layer and presentation layer are properly define and implemented,
but when i try to write test for my code i couldn’t able to do that maybe because im creating states in different way and also cant make states equatable because im already generating code through built value and its not allowing it,
so if i cant write test cases by using this method, then what is the best approach to do that?
my test case is
import 'login_test.mocks.dart';
@GenerateMocks([LoginUseCase])
void main() {
late LoginBloc loginBloc;
late MockLoginUseCase mockLoginUseCase;
setUp(() {
mockLoginUseCase = MockLoginUseCase();
loginBloc = LoginBloc(loginUseCase: mockLoginUseCase);
});
group('LoginBloc Tests', () {
final email = '[email protected]';
final password = 'password';
final loginParams = LoginParams(email: email, password: password);
final userEntity = UserEntity(id: 1, email: email, name: 'Test User'); // Adjust based on your UserEntity
blocTest<LoginBloc, LoginState>(
'emits [isLoading: true, isSuccess: false, error: ""] then [isLoading: false, isSuccess: true, user: userEntity, error: ""] when LoginUserEvent is added and login is successful',
setUp: () {
when(mockLoginUseCase.execute(params: loginParams))
.thenAnswer((_) async => Right(userEntity));
},
build: () => loginBloc,
act: (bloc) => bloc.add(LoginUserEvent(params: loginParams)),
expect: () => [
LoginState.initial().rebuild((b) => b
..isLoading = true
..isSuccess = false
..error = ''),
LoginState.initial().rebuild((b) => b
..isLoading = false
..isSuccess = true
..user = userEntity
..error = ''),
],
);
blocTest<LoginBloc, LoginState>(
'emits [isLoading: true, isSuccess: false, error: ""] then [isLoading: false, isSuccess: false, error: "Login Failed"] when LoginUserEvent is added and login fails',
setUp: () {
when(mockLoginUseCase.execute(params: loginParams))
.thenAnswer((_) async => Left(ErrorModel(statusCode: 400, status: "error", message: "Login Failed")));
},
build: () => loginBloc,
act: (bloc) => bloc.add(LoginUserEvent(params: loginParams)),
expect: () => [
LoginState.initial().rebuild((b) => b
..isLoading = true
..isSuccess = false
..error = ''),
LoginState.initial().rebuild((b) => b
..isLoading = false
..isSuccess = false
..error = 'Login Failed'),
],
);
});
}
and the error code im facing is
Error resuming isolate isolates/2042399400965491: 106 Isolate must be paused
r
package:bloc_test/src/bloc_test.dart 236:7 testBloc
Expected: [
_$LoginState:LoginState {
isSuccess=false,
isLoading=true,
error=,
user=Instance of 'UserEntity',
isOffline=false,
},
_$LoginState:LoginState {
isSuccess=true,
isLoading=false,
error=,
user=Instance of 'UserEntity',
isOffline=false,
}
]
Actual: [
_$LoginState:LoginState {
isSuccess=false,
isLoading=true,
error=,
user=Instance of 'UserEntity',
isOffline=false,
},
_$LoginState:LoginState {
isSuccess=true,
isLoading=false,
error=,
user=Instance of 'UserEntity',
isOffline=false,
}
]
Which: at location [0] is _$LoginState:<LoginState {
isSuccess=false,
isLoading=true,
error=,
user=Instance of 'UserEntity',
isOffline=false,
}> instead of _$LoginState:<LoginState {
isSuccess=false,
isLoading=true,
error=,
user=Instance of 'UserEntity',
isOffline=false,
}>
WARNING: Please ensure state instances extend Equatable, override == and hashCode, or implement Comparable.
Alternatively, consider using Matchers in the expect of the blocTest rather than concrete state instances.