Home > Blockchain >  I'm having trouble testing with null-safety
I'm having trouble testing with null-safety

Time:06-02

I'm trying to run a test in dart and I'm getting the following return:

type 'Null' is not a subtype of type 'Future<Either<FailureLogin, LoginUser>>'

login_with_user_test.dart

import 'package:dartz/dartz.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:ola_desktop/modules/login/domain/entities/login_user.dart';
import 'package:ola_desktop/modules/login/domain/repositories/login_repository.dart';
import 'package:ola_desktop/modules/login/domain/usecases/login_with_user.dart';

class LoginRepositoryMock extends Mock implements LoginRepository {}

main() {
  final repository = LoginRepositoryMock();
  final usecase = LoginWithUserImpl(repository);

  test('Deve retornar um LoginUser', () async {
    // ignore: prefer_const_constructors
    when(repository.login(user: '', password: ''))
        .thenAnswer((_) async => Right(LoginUser.empty()));

    final result = await usecase("gabriel.ribeiro", "123456");
    expect(result, isA<LoginUser>());
  });
}

login_repository.dart

import 'package:dartz/dartz.dart';

import '../entities/login_user.dart';
import '../errors/errors.dart';

abstract class LoginRepository {
  Future<Either<FailureLogin, LoginUser>> login(
      {required String user, required String password});
}

login_with_user.dart

import 'package:dartz/dartz.dart';

import '../entities/login_user.dart';
import '../errors/errors.dart';
import '../repositories/login_repository.dart';

abstract class LoginWithUser {
  Future<Either<FailureLogin, LoginUser>> call(String user, String password);
}

class LoginWithUserImpl implements LoginWithUser {
  final LoginRepository repository;

  LoginWithUserImpl(this.repository);

  @override
  Future<Either<FailureLogin, LoginUser>> call(String user, String password) {
    return repository.login(user: user, password: password);
  }
}

login_user.dart

class LoginUser {
  final String user;
  final String password;

  LoginUser({required this.user, required this.password});

  factory LoginUser.empty() {
    return LoginUser(user: '', password: '');
  }
}

errors.dart

abstract class FailureLogin implements Exception {}

Print explorer folder Lib

Print explorer folder Test

CodePudding user response:

I am not totaly sure but maybe is releated with Either<FailureLogin, LoginUser>, can you rewrite the test codes with result.isRight(), if it is works re check the expect(result, isA()); line. because result can be Right or left but is Either type.

also can you check the repository.login(user: '', password: '') replacing with await repository.login(user: '', password: '')

CodePudding user response:

Great afternoon!

Manage to solve, this extends Mock is thing before nullsafety after nullsafety this no longer works. Had to use GenerateMock

class test

enter image description here

Then I ran the code in the terminal to generate class mocks:

flutter pub run build_runner build
  • Related