Dartz,颤动测试 - 无法获得 Future<Either<L, R>> 的正确部分,而不是 - Future<Either<L, R>> 的实例作为实际结果
Dartz, flutter test - Can't get a Right part of Future<Either<L, R>>, instead of that - instance of Future<Either<L, R>> as a actual result
这是我第一次尝试编写测试。我使用 Dartz 包,但我不知道如何只获得一个部件作为我的测试结果。错误是:
Expected: Right<dynamic, NumberTrivia>:<Right(NumberTrivia(test, 1))>
Actual: <Instance of 'Future<Either<Failure, NumberTrivia>>'>
存储库中的函数:
abstract class NumberTriviaRepository {
Future<Either<Failure, NumberTrivia>> getConcreteNumberTrivia(int number);
Future<Either<Failure, NumberTrivia>> getRandomNumberTrivia();
}
用例中的函数:
class GetConcreteNumberTrivia {
final NumberTriviaRepository numberTriviaRepository;
GetConcreteNumberTrivia(this.numberTriviaRepository);
Future<Either<Failure, NumberTrivia>> execute({
required int number,
}) async {
return await numberTriviaRepository.getConcreteNumberTrivia(number);
}
}
和测试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);
});
}
我的 git 代码:
https://github.com/VeronikaHaichenko/clean_architecture_education
出现这个问题是因为usecase.execute(number: tNumber);
重新调整future,在它之前使用await
。
// act
final result = await usecase.execute(number: tNumber);
这是我第一次尝试编写测试。我使用 Dartz 包,但我不知道如何只获得一个部件作为我的测试结果。错误是:
Expected: Right<dynamic, NumberTrivia>:<Right(NumberTrivia(test, 1))> Actual: <Instance of 'Future<Either<Failure, NumberTrivia>>'>
存储库中的函数:
abstract class NumberTriviaRepository {
Future<Either<Failure, NumberTrivia>> getConcreteNumberTrivia(int number);
Future<Either<Failure, NumberTrivia>> getRandomNumberTrivia();
}
用例中的函数:
class GetConcreteNumberTrivia {
final NumberTriviaRepository numberTriviaRepository;
GetConcreteNumberTrivia(this.numberTriviaRepository);
Future<Either<Failure, NumberTrivia>> execute({
required int number,
}) async {
return await numberTriviaRepository.getConcreteNumberTrivia(number);
}
}
和测试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);
});
}
我的 git 代码: https://github.com/VeronikaHaichenko/clean_architecture_education
出现这个问题是因为usecase.execute(number: tNumber);
重新调整future,在它之前使用await
。
// act
final result = await usecase.execute(number: tNumber);