1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to test two providers in Flutter / Riverpod?

Discussão em 'Mobile' iniciado por Stack, Novembro 6, 2024 às 13:42.

  1. Stack

    Stack Membro Participativo

    I have 2 providers, Provider1 and Provider2, where Provider1 uses Provider2 in this way:

    @riverpod
    class Provider1Notifier extends _$Provider1Notifier with PersistenceHelper {

    ....

    void buy(int id) {
    final ProviderData data = getData(id);
    ...
    }

    ProviderData getData(int id) => ref.read(provider2NotifierProvider).firstWhere((g) => g.id == id);
    }


    In my test for Provider1, I have something like:

    class MockProvider1Notifier extends Provider1Notifier {
    @override
    ProviderData build() {
    return ProviderData(
    ...
    );
    }
    }

    void main() {
    group('Provider1Notifier.init', ()
    {
    test('Do stuff...', () {
    final container = createContainer(overrides: [
    provider1NotifierProvider.overrideWith(() => MockProvider1Notifier()),
    provider2Provider1NotifierProvider.overrideWith(() => Provider2Notifier()),
    ]);

    Provider1Notifier notifier = container.read(provider1NotifierProvider.notifier);
    notifier.doSomething(0);
    ...
    }
    }
    }


    When I run the test I get the error:


    UnimplementedError

    which is pointing to the line of code:

    ProviderData getData(int id) => ref.read(provider2NotifierProvider).firstWhere((g) => g.id == id);


    and in particular to ref.read(provider2NotifierProvider).

    I do not see more error description than that. Is there anything wrong with the test setting? How to fix that?

    Continue reading...

Compartilhe esta Página