Skip to content

Instantly share code, notes, and snippets.

@StanislawNagorski
Last active January 9, 2023 10:17
Show Gist options
  • Save StanislawNagorski/42005bc584d2356328fb4d5f13bbad75 to your computer and use it in GitHub Desktop.
Save StanislawNagorski/42005bc584d2356328fb4d5f13bbad75 to your computer and use it in GitHub Desktop.
mock remote data source
const String _mockDataJson = 'assets/mock_data/events_spots_localization_mock_data.json';
const int _mockCallMillisecondsDuration = 500;
@Injectable(
as: RemoteDataSourceMock,
env: [Flavor.DEVELOP, Flavor.STAGE],
)
class RemoteDataSourceMock implements RemoteDataSource {
@override
Future<List<DataModel>> getData({required GeoPoint userLocation}) async =>
await Future.delayed(Duration(milliseconds: _mockCallMillisecondsDuration))
.then((_) => File(_mockDataJson)
.readAsString()
.then((json) => jsonDecode(json))
.then((data) => (data as Iterable)
.map((data) => DataModel.fromJson(data))
.toList())
);
}
/// TEST
void main() {
group('json decoding', () {
setUpAll(() => WidgetsFlutterBinding.ensureInitialized());
test('should decode json', () async {
//Given
final fakeUserLocation = GeoPoint(0, 0);
final mockDataSource = RemoteDataSourceMock();
//When
final result = await mockDataSource.getData(userLocation: fakeUserLocation);
//Then
expect(result, isList);
});
test('return list should not be empty', () async {
//Given
final fakeUserLocation = GeoPoint(0, 0);
final mockDataSource = RemoteDataSourceMock();
//When
final result = await mockDataSource.getData(userLocation: fakeUserLocation);
//Then
expect(result, isNotEmpty);
});
test('should map json to Spot Location Model', () async {
//Given
final fakeUserLocation = GeoPoint(0, 0);
final mockDataSource = RemoteDataSourceMock();
//When
final responseData = await mockDataSource.getData(userLocation: fakeUserLocation);
final result = responseData.every((element) => element is DataModel);
//Then
expect(result, isTrue);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment