Reputation: 29
I'm testing a repository implementation but the test fails and I can't find where the error is.
This is the repository:
class ProductRepositoryImpl implements ProductRepository {
final ProductRemoteDataSource remoteDataSource;
final NetworkInfo networkInfo;
ProductRepositoryImpl(
{@required this.remoteDataSource, @required this.networkInfo});
@override
Future<Either<Failure, List<Product>>> getProducts() async {
this.networkInfo.istConnected;
return Right(await this.remoteDataSource.getProducts());
}
}
This is the test implementation:
class MockRemoteDataSource extends Mock implements ProductRemoteDataSource {}
class MockNetworkInfo extends Mock implements NetworkInfo {}
void main() {
ProductRepositoryImpl repository;
MockRemoteDataSource mockRemoteDataSource;
MockNetworkInfo mockNetworkInfo;
final tProductModel = ProductModel(
id: 1,
title: 'Product 1',
description: 'Product description',
oldPrice: '20.99',
discount: 10,
storeName: 'Zé Strore',
remainingOffers: 3,
imagesUrls: ['https://unsplash.com/photos/EF7BVa9BB2M']);
final productModelList = [tProductModel];
final Product tProduct = tProductModel;
final productList = [tProduct];
setUp(() {
mockRemoteDataSource = MockRemoteDataSource();
mockNetworkInfo = MockNetworkInfo();
repository = ProductRepositoryImpl(
remoteDataSource: mockRemoteDataSource, networkInfo: mockNetworkInfo);
});
group('device is online', () {
setUp(() {
when(mockNetworkInfo.istConnected).thenAnswer((_) async => true);
});
test(
'should return remote data when the call to remote data source is successful',
() async {
// arrange
when(mockRemoteDataSource.getProducts())
.thenAnswer((_) async => productModelList);
// act
final result = await repository.getProducts();
// assert
verify(mockRemoteDataSource.getProducts());
expect(result, equals(Right(productList)));
});
});
}
The error reported is:
Expected: Right<dynamic, List<Product>>:<Right([ProductModel(1, Product 1, Product description, 20.99, 10, Zé Strore, 3, [https://unsplash.com/photos/EF7BVa9BB2M])])>
Actual: Right<Failure, List<Product>>:<Right([ProductModel(1, Product 1, Product description, 20.99, 10, Zé Strore, 3, [https://unsplash.com/photos/EF7BVa9BB2M])])>
package:test_api expect
expect
package:flutter_test/src/widget_tester.dart:441
main.<fn>.<fn>
test/…/repositories/product_repository_impl_test.dart:74
I don't know where is the error. The objects looks like the same. I tried to use expect(result, equals(Right<Failure, List<Product>>(productList)));
, but the error persists. The dataSource object has one method and was mocked.
Upvotes: 0
Views: 1183
Reputation: 1934
I always did this using the fold method:
final Either<Failure, List<Product>> result = await repository.getProducts();
result.fold((left) => fail('test failed'), (right) {
verify(mockRemoteDataSource.getProducts()).called(1);
expect(right, equals(productList));
});
Don't know if that's the correct way to do it, but for me it worked. Couldn't check it. If it didn't work please let me know!
Upvotes: 3