This is my first try to write a test. I use the Dartz package and I don't know how to get just one part as a result of my test. The error is:
Expected: Right<dynamic, NumberTrivia>:<Right(NumberTrivia(test, 1))> Actual: <Instance of 'Future<Either<Failure, NumberTrivia>>'>
The function in repository:
abstract class NumberTriviaRepository {
Future<Either<Failure, NumberTrivia>> getConcreteNumberTrivia(int number);
Future<Either<Failure, NumberTrivia>> getRandomNumberTrivia();
}
The function in usecase:
class GetConcreteNumberTrivia {
final NumberTriviaRepository numberTriviaRepository;
GetConcreteNumberTrivia(this.numberTriviaRepository);
Future<Either<Failure, NumberTrivia>> execute({
required int number,
}) async {
return await numberTriviaRepository.getConcreteNumberTrivia(number);
}
}
And the test class:
import 'package:dartz/dartz.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:number_trivia/features/number_trivia/domain/entities/number_trivia.dart';
import 'package:number_trivia/features/number_trivia/domain/repositories/number_trivia_repository.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:number_trivia/features/number_trivia/domain/usecases/get_concrete_number_trivia.dart';
import 'get_concrete_number_trivia_test.mocks.dart';
@GenerateMocks([NumberTriviaRepository])
void main() {
late GetConcreteNumberTrivia usecase;
late MockNumberTriviaRepository mockNumberTriviaRepository;
setUp(() {
mockNumberTriviaRepository = MockNumberTriviaRepository();
usecase = GetConcreteNumberTrivia(mockNumberTriviaRepository);
});
final tNumber = 1;
final tNumberTrivia = NumberTrivia(text: 'test', number: tNumber);
test('should get trivia for the number from the repository', () async {
// arrange
when(mockNumberTriviaRepository.getConcreteNumberTrivia(tNumber))
.thenAnswer((_) async => Right(tNumberTrivia));
// act
final result = usecase.execute(number: tNumber);
// assets
expect(result, Right(tNumberTrivia));
verify(mockNumberTriviaRepository.getConcreteNumberTrivia(tNumber));
verifyNoMoreInteractions(mockNumberTriviaRepository);
});
}
My git with code: https://github.com/VeronikaHaichenko/clean_architecture_education
CodePudding user response:
The issue coming because usecase.execute(number: tNumber);
retuning future, use await
before it.
// act
final result = await usecase.execute(number: tNumber);