它似乎与matcher具有相同的值,但仍然无法通过测试,可能是因为内存地址之类的东西。有人能让我知道当列表在Either<< Right >>中时如何测试结果吗?
test('get board list from remote data source', () async {
when(mockBoardRemoteDataSource.getBoards())
.thenAnswer((_) async => tBoardModels);
final result = await repository.getBoards();
verify(mockBoardRemoteDataSource.getBoards());
expect(result, equals(Right(toBoards)));
// Either<Failure, List<BoardInfo>> result;
// (new) Right<dynamic, List<BoardInfo>> Right(List<BoardInfo> _r)
});//控制台结果
Expected: Right<dynamic, List<BoardInfo>>:<Right([_$_BoardInfo(1, name1, address1), _$_BoardInfo(2, name2, address2)])>
Actual: Right<Failure, List<BoardInfo>>:<Right([_$_BoardInfo(1, name1, address1), _$_BoardInfo(2, name2, address2)])>
package:test_api expect
package:flutter_test/src/widget_tester.dart 455:16 expect
test\features\nurban_honey\data\repositories\board_repository_impl_test.dart 58:9 main.<fn>.<fn>.<fn>//BoardInfo执行情况
import 'package:equatable/equatable.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'board_info.freezed.dart';
@freezed
class BoardInfo extends Equatable with _$BoardInfo {
BoardInfo._();
factory BoardInfo(int id, String name, String address) = _BoardInfo;
@override
List<Object?> get props => [id, name, address];
}发布于 2022-03-29 00:05:15
我通过这样做通过了测试:
result.fold(
(l) => null,
(resultR) => Right(toBoards)
.fold((l) => null, (matcherR) => expect(resultR, matcherR)));有更好的方法吗?
发布于 2022-11-10 15:09:08
多亏了杰伊,他的回答帮助我下了决心。
我就是这样做测试的:
// Act
var results = (await repository.fetch()).fold(
(failure) => failure,
(response) => response,
);然后,我做了一个类型期望,与在我的用例上声明的失败类型相匹配:
在成功执行的情况下
// Assert
expect(results, isA<ResultType>());在失败期望的情况下
// Assert
expect(results, isA<FailureType>());整个测试用例如下所示
test('On successful execution, should returns a SuccessResultType', () async {
// Arrange
repository = MyUseCaseRepository();
// Act
var results = (await repository.fetch()).fold(
(failure) => failure,
(response) => response,
);
// Assert
expect(results, isA<SuccessResultType>());
});我希望这种方法能帮助到任何人!
https://stackoverflow.com/questions/71644737
复制相似问题