What is the proper way to unittest future errors in flutter/dart?

Issue

The unittest that I am writing should fail on exception and I would like to match the error thrown using a matcher, but I am getting an error.

What am I doing wrong currently and what is the error here?
If I am doing it incorrectly, what is the correct way to test the error scenario for a method that returns a future? And why is there an asynchronous gap?

Here’s the method I want to test :

Future<void> registerUser(String name, String email, String password) async {
auth.signUp(email, password, name).then((_) {
      auth.getCurrentUser().then((user) async {
        // Doing something
    }).catchError((onError) {
      throw onError;
    });

Here’s the test I have written :

test('should fail to register if fetching user fails', () async {
      MockAuth auth = MockAuth();
      RegisterRepository repo = RegisterRepository(auth);

      String password = 'password';
      String email = 'email';
      String name = 'name';

      when(auth.signUp(email, password, name))
          .thenAnswer((_) => Future.value());
      when(auth.getCurrentUser()).thenThrow((_) => throw Error());

      try {
        await repo.registerUser(name, email, password);
        fail('exception not thrown');
      } catch (e) {}

      verify(auth.signUp(email, password, name)).called(1);
      verify(auth.getCurrentUser()).called(1);
      verifyNoMoreInteractions(auth);
    });

I am getting this error :

package:mockito/src/mock.dart 403:7               PostExpectation.thenThrow.<fn>
package:mockito/src/mock.dart 130:45              Mock.noSuchMethod
package:dive/repository/register_repo.dart 25:12  RegisterRepository.registerUser.<fn>
===== asynchronous gap ===========================
dart:async                                        Future.then
package:dive/repository/register_repo.dart 24:40  RegisterRepository.registerUser
test/repository/register_repo_test.dart 61:20     main.<fn>.<fn>

Closure: (dynamic) => Null

Solution

For anyone else who faces this problem in the future,

  1. I removed the async keyword from the method that is being tested, async keyword is not necessary here. Chaining the calls makes it more readable :
Future<void> registerUser(String name, String email, String password) {
return auth
        .signUp(email, password, name)
        .then((_) => auth.getCurrentUser())
        .then((user) {
        // Doing something
    }).catchError((onError) {
      throw onError;
    });
  1. To test the error from the future, do the following :
test('should fail to register if fetching user fails', () async {
      MockAuth auth = MockAuth();
      RegisterRepository repo = RegisterRepository(auth);

      String password = 'password';
      String email = 'email';
      String name = 'name';

      when(auth.signUp(email, password, name))
          .thenAnswer((_) => Future.value());
      when(auth.getCurrentUser()).thenAnswer((_) => Future.error('error'));

      repo.registerUser(name, email, password).catchError((onError) {
        expect(onError.toString(), 'error');

        verify(auth.signUp(email, password, name)).called(1);
        verify(auth.getCurrentUser()).called(1);
        verifyNoMoreInteractions(auth);
      });
    });

The tests pass now.

Answered By – Prajval Prabhakar

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.