Last active
January 12, 2020 08:06
-
-
Save rafdls/8db694428cb599af4085161e5849742e to your computer and use it in GitHub Desktop.
location repository implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| when(mockGeolocator.getCurrentPosition( | |
| desiredAccuracy: anyNamed('desiredAccuracy'), | |
| locationPermissionLevel: anyNamed('locationPermissionLevel'))) | |
| .thenAnswer((_) { | |
| return Future<Position>.value(Position(latitude: 20, longitude: 30)); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter_unit_testing/bloc/map_coordinate.dart'; | |
| import 'package:flutter_unit_testing/repository/location_repository.dart'; | |
| import 'package:geolocator/geolocator.dart'; | |
| class GeolocatorRepository implements LocationRepository { | |
| final Geolocator _geolocator; | |
| GeolocatorRepository(this._geolocator); | |
| @override | |
| Future<MapCoordinate> getCurrentLocation() { | |
| return _geolocator | |
| .getCurrentPosition(desiredAccuracy: LocationAccuracy.low) | |
| .then((position) { | |
| return MapCoordinate(position.latitude, position.longitude); | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter_test/flutter_test.dart'; | |
| import 'package:flutter_unit_testing/bloc/map_coordinate.dart'; | |
| import 'package:flutter_unit_testing/repository/geolocator_repository.dart'; | |
| import 'package:geolocator/geolocator.dart'; | |
| import 'package:mockito/mockito.dart'; | |
| class MockGeolocator extends Mock implements Geolocator {} | |
| void main() { | |
| test('should fetch current location', () async { | |
| final Geolocator mockGeolocator = MockGeolocator(); | |
| when(mockGeolocator.getCurrentPosition( | |
| desiredAccuracy: anyNamed('desiredAccuracy'), | |
| locationPermissionLevel: anyNamed('locationPermissionLevel'))) | |
| .thenAnswer((_) { | |
| return Future<Position>.value(Position(latitude: 20, longitude: 30)); | |
| }); | |
| final GeolocatorRepository geolocatorRepository = | |
| GeolocatorRepository(mockGeolocator); | |
| MapCoordinate coordinateFuture = | |
| await geolocatorRepository.getCurrentLocation(); | |
| expect(coordinateFuture, equals(MapCoordinate(20, 30))); | |
| verify(mockGeolocator.getCurrentPosition( | |
| desiredAccuracy: anyNamed('desiredAccuracy'), | |
| locationPermissionLevel: anyNamed('locationPermissionLevel'))); | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MockGeolocator extends Mock implements Geolocator {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final GeolocatorRepository geolocatorRepository = GeolocatorRepository(mockGeolocator); | |
| MapCoordinate coordinateFuture = await geolocatorRepository.getCurrentLocation(); | |
| expect(coordinateFuture, equals(MapCoordinate(20, 30))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| verify(mockGeolocator.getCurrentPosition( | |
| desiredAccuracy: anyNamed('desiredAccuracy'), | |
| locationPermissionLevel: anyNamed('locationPermissionLevel'))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment