form_bloc icon indicating copy to clipboard operation
form_bloc copied to clipboard

how to test form_bloc with BlocMock and MockRepository?

Open hansbak opened this issue 4 years ago • 5 comments

Could you give us an example how to test the form and the bloc separately in the form_bloc organization?

Regards, Hans

hansbak avatar Apr 23 '20 08:04 hansbak

This is a serious question. If anyone got at least one great example, I'm more than glad to look at it

IzioDev avatar Oct 27 '20 13:10 IzioDev

Hello everyone, Any news ?

I am new to flutter Testing and I only manage to test a simple thing but I don't know how to test the value sent from a TextField or to access the state.

This my test bloc :

class MockPasswordForgotFormBloc extends MockBloc<FormBlocEvent, FormBlocState<String, String>> 
implements PasswordForgotFormBloc {}


void main() {

  group("Unit test PasswordForgotFormBloc", () {

    blocTest<PasswordForgotFormBloc,FormBlocState<String, String>>(
      "initial state",
      build: () => PasswordForgotFormBloc(),
      expect: () => [
           UpdateFormBlocState(FormBlocLoaded()) // this fails because of toJson & fieldBlocs
      ] 
    );

    blocTest<PasswordForgotFormBloc,FormBlocState<String, String>>(
      "when user submit nothing",
      build: () => PasswordForgotFormBloc(),
      act: (bloc) => bloc.submit(),
      verify: (bloc) {
        return bloc.state.isValid() == false;
      }
    );
    
  });
}

This is my PasswordForgotFormBloc :

class PasswordForgotFormBloc extends FormBloc<String, String> {
  final email = TextFieldBloc(
    validators: [
      FieldBlocValidators.required,
      FieldBlocValidators.email,
    ],
  );

  PasswordForgotFormBloc() {
    addFieldBlocs(fieldBlocs: [email]);
  }
  
  @override
  void onSubmitting() async {
    // TODO : add connexion to firebase or any other database to login

    try {
      await Future<void>.delayed(const Duration(milliseconds: 500));
      emitSuccess();
    } catch (e) {
      emitFailure();
    }
  }
}

So does anyone have an answer after 2 years ^^ ?

nicolasvanheusden avatar Feb 28 '22 09:02 nicolasvanheusden

@nicolasvanheusden https://pub.dev/packages/flutter_floc

IzioDev avatar Feb 28 '22 12:02 IzioDev

@IzioDev Ooh Ok flutter_form_bloc is not maintained anymore. I see thx !

nicolasvanheusden avatar Feb 28 '22 12:02 nicolasvanheusden

Hello there, any updates on how to add test to forms?

aminbenarieb avatar Jul 12 '22 15:07 aminbenarieb