我正在尝试将干净的体系结构和TDD纳入我的项目。因此,我正在测试用例是否返回期望的数据。但是下面的测试用例对我来说是失败的。
void main() {
GetWordDescription useCase;
WordRepository repository;
setUp(() {
repository = new MockGetWordDescription();
useCase = new GetWordDescription(repository);
});
final tWord = 'lexicon';
final tWordMap = /* word class initialization */;
//fixme test cases failing
test('should get List of words from the repository', () async {
when(repository.getAll())
.thenAnswer((realInvocation) async => Right([tWordMap]));
final result = await useCase.getAll();
expect(result, equals(Right<Failure, List<Word>>([tWordMap])));
verify(repository.getAll());
verifyNoMoreInteractions(repository);
});
}
而且我不太确定自己在做什么错。Word类是简单的Equatable类的实例。
class Word extends Equatable {
/* class fields */
@override
List<Object> get props => [/* class fields */];
}
它显示以下输出
Expected: Right<Failure, List<Word>>:<Right([Word])>
Actual: Right<Failure, List<Word>>:<Right([Word])>
列表的两个实例不相等。因此,repository.getAll()返回的[tWord]与usecase.getAll()返回的[tWord]不相等。
我通过将列表提取到变量中并在两种情况下都使用它来解决此问题。
var tAllBooks = [tWordMap,/* more books */];
test('should get List of words from the repository', () async {
when(repository.getAll())
.thenAnswer((realInvocation) async => Right(tAllBooks));
final result = await useCase.getAll();
expect(result, equals(Right(tAllBooks)));
verify(repository.getAll());
verifyNoMoreInteractions(repository);
});