Bloc: [Question] Testing LoginState in Firebase Login Tutorial Example

Created on 4 May 2020  路  2Comments  路  Source: felangel/bloc

From the example Firebase Login Tutorial, when testing if the initial state is correct:

test('initial state is correct', () {
expect(LoginState.empty(), loginBloc.initialState);
});

The test failed with:

package:test_api expect
package:flutter_test/src/widget_tester.dart 234:3 expect
test/login_bloc_test.dart 37:5 main.

Expected: LoginState: isEmailValid: true,
isPasswordValid: true,
isSubmitting: false,
isSuccess: false,
isFailure: false,
}>
Actual: LoginState: isEmailValid: true,
isPasswordValid: true,
isSubmitting: false,
isSuccess: false,
isFailure: false,
}>

Similarly, when testing of 'LoginButtonPressed':

group('LoginButtonPressed', () {
blocTest(
'emits [LoginLoading, LoginInitial]',
build: () async {
when(userRepository.signInWithCredentials(
'valid.email', 'valid.password'))
.thenAnswer((_) => Future.value(firebaseUserMock.email));
return loginBloc;
},
act: (bloc) => bloc.add(
LoginWithCredentialsPressed(
email: '[email protected]',
password: 'password',
),
),
expect: [
LoginState.loading(),
LoginState.success(),
],
);
});

The test failed with:

package:test_api expect
package:bloc_test/src/bloc_test.dart 124:25 blocTest.

Expected: [
LoginState:LoginState {
isEmailValid: true,
isPasswordValid: true,
isSubmitting: true,
isSuccess: false,
isFailure: false,
},
LoginState:LoginState {
isEmailValid: true,
isPasswordValid: true,
isSubmitting: false,
isSuccess: true,
isFailure: false,
}
]
Actual: [
LoginState:LoginState {
isEmailValid: true,
isPasswordValid: true,
isSubmitting: true,
isSuccess: false,
isFailure: false,
},
LoginState:LoginState {
isEmailValid: true,
isPasswordValid: true,
isSubmitting: false,
isSuccess: true,
isFailure: false,
}
]
Which: was LoginState: isEmailValid: true,
isPasswordValid: true,
isSubmitting: true,
isSuccess: false,
isFailure: false,
}> instead of LoginState: isEmailValid: true,
isPasswordValid: true,
isSubmitting: true,
isSuccess: false,
isFailure: false,
}> at location [0]

Would this be the correct way to test it? Any help or documentation with regards to this seemingly trivial matter would be greatly appreciated ! Thanks in advance !

question

Most helpful comment

Thanks @felangel ! 馃憤 , totally missed that. Perfect !

All 2 comments

Hi @gunsym 馃憢
Thanks for opening an issue!

I鈥檓 guessing your state does not extend Equatable which is why two state instances with the same values are not considered equal.

Hope that helps 馃憤

Thanks @felangel ! 馃憤 , totally missed that. Perfect !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craiglabenz picture craiglabenz  路  3Comments

hivesey picture hivesey  路  3Comments

MahdiPishguy picture MahdiPishguy  路  3Comments

clicksocial picture clicksocial  路  3Comments

Reidond picture Reidond  路  3Comments