2021年5月7日星期五

Flutter - Using async in testing produces an error but using async* made it work fine?

In short, I'm following the ResoCoder TDD Course. The course is a bit outdated but I'm pretty sure it's fine to follow along and make adjustments along the way. The course and the code in GitHub for this course is not null-safety.

And I'm not following the course exactly. I made a few changes here and there.

I'm just into the course at about episode 2 or 3 for context.

auth_usecases_test.dart

import 'package:dartz/dartz.dart';  import 'package:mockito/mockito.dart';  import 'package:storayge/core/auth/domain/entities/local_user.dart';  import 'package:storayge/core/auth/domain/repository/auth_repository.dart';  import 'package:flutter_test/flutter_test.dart';  import 'package:storayge/core/auth/domain/usecases/auth_usecases.dart';    class MockAuthRepository extends Mock implements AuthRepository {}    void main() {    late GetLocalUserDataFromRemote usecase;    late MockAuthRepository mockAuthRepository;      late String tUid;    late LocalUser tLocalUser;      setUp(() {      mockAuthRepository = MockAuthRepository();      usecase = GetLocalUserDataFromRemote(repository: mockAuthRepository);      tLocalUser = const LocalUser(        username: 'testUsername',        email: 'testEmail@Email',        uid: 'testUid',      );      tUid = 'testUid';    });      test(      'should get LocalUser data from the repository',      () async* {        // arrange        when(mockAuthRepository.getLocalUserDataFromRemote(uid: tUid))            .thenAnswer((_) async => Right(tLocalUser));        // act        final result = await usecase(Params(uid: tUid));        // assert        expect(result, equals(Right(tLocalUser)));        verify(mockAuthRepository.getLocalUserDataFromRemote(uid: tUid));        verifyNoMoreInteractions(mockAuthRepository);      },    );  }  

This is the "working" code. If I change the async* keyword to just async it produces this error:

type 'Null' is not a subtype of type 'Future<Either<Failure, LocalUser>>'  

auth_usecases.dart

class GetLocalUserDataFromRemote implements Usecase<LocalUser, Params> {    final AuthRepository repository;      GetLocalUserDataFromRemote({required this.repository});      @override    Future<Either<Failure, LocalUser>> call(Params params) async {      return repository.getLocalUserDataFromRemote(uid: params.uid);    }  }    class Params extends Equatable {    final String uid;      Params({required this.uid});      @override    List<Object?> get props => [uid];  }  

auth_repository.dart

import 'package:dartz/dartz.dart';    import '../../../errors/failures.dart';  import '../entities/local_user.dart';    abstract class AuthRepository {    Future<Either<Failure, LocalUser>> getLocalUserDataFromRemote({      required String uid,    });      Future<Either<Failure, LocalUser>> signInWithEmailAndPassword({      required String email,      required String password,    });  }  

local_user.dart

class LocalUser extends Equatable {    final String username;    final String email;    final String uid;      const LocalUser({      required this.username,      required this.email,      required this.uid,    });      @override    List<Object?> get props => [username, email, uid];  }  

failures.dart

abstract class Failure extends Equatable {    final List<Object> properties;      const Failure({required this.properties});      @override    List<Object> get props => properties;  }  

Why and how does changing the async tag make it work? I'm stumped.

https://stackoverflow.com/questions/67443531/flutter-using-async-in-testing-produces-an-error-but-using-async-made-it-work May 08, 2021 at 10:28AM

没有评论:

发表评论