Skip to content

Instantly share code, notes, and snippets.

@rafdls
Last active January 17, 2020 11:43
Show Gist options
  • Save rafdls/f0bb6af72adbd2e841535b539fabd8c2 to your computer and use it in GitHub Desktop.
Save rafdls/f0bb6af72adbd2e841535b539fabd8c2 to your computer and use it in GitHub Desktop.
weather app bloc
final expectedWeatherModel = WeatherModel(20, 'Sydney', 'Sunny', Icons.cloud, []);
final mapCoordinate = MapCoordinate(1.0, -1.0);
when(mockLocationRepository.getCurrentLocation())
.thenAnswer((_) {
return Future.value(mapCoordinate);
});
when(mockWeatherRepository.getWeatherWithLocation(mapCoordinate))
.thenAnswer((_) {
return Future.value(expectedWeatherModel);
});
WeatherBloc weatherBloc = WeatherBloc(mockWeatherRepository, mockLocationRepository);
weatherBloc.inputWeatherEvent.add(FetchWeatherEvent());
expectLater(weatherBloc.weatherModel, emits(expectedWeatherModel))
.then((_) {
verify(mockLocationRepository.getCurrentLocation());
verify(mockWeatherRepository.getWeatherWithLocation(mapCoordinate));
});
import 'dart:async';
import 'package:flutter_unit_testing/bloc/fetch_weather_event.dart';
import 'package:flutter_unit_testing/bloc/weather_model.dart';
import 'package:flutter_unit_testing/repository/location_repository.dart';
import 'package:flutter_unit_testing/repository/weather_repository.dart';
class WeatherBloc {
final _weatherStateController = StreamController<WeatherModel>();
final _weatherEventController = StreamController<FetchWeatherEvent>();
final WeatherRepository _weatherRepository;
final LocationRepository _locationRepository;
WeatherBloc(this._weatherRepository, this._locationRepository) {
_weatherEventController.stream.listen(_handleEvent);
}
Stream<WeatherModel> get weatherModel => _weatherStateController.stream;
Sink<FetchWeatherEvent> get inputWeatherEvent => _weatherEventController.sink;
void _handleEvent(FetchWeatherEvent event) async {
try {
final currentLocation = await _locationRepository.getCurrentLocation();
final weather = await _weatherRepository.getWeatherWithLocation(currentLocation);
_weatherStateController.sink.add(weather);
} catch (e) {
_weatherStateController.sink.addError(e);
}
}
void dispose() {
_weatherStateController.close();
_weatherEventController.close();
}
}
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_unit_testing/bloc/fetch_weather_event.dart';
import 'package:flutter_unit_testing/bloc/map_coordinate.dart';
import 'package:flutter_unit_testing/bloc/weather_bloc.dart';
import 'package:flutter_unit_testing/bloc/weather_model.dart';
import 'package:flutter_unit_testing/repository/location_repository.dart';
import 'package:flutter_unit_testing/repository/weather_repository.dart';
import 'package:mockito/mockito.dart';
class MockWeatherRepository extends Mock implements WeatherRepository {}
class MockLocationRepository extends Mock implements LocationRepository {}
void main() {
group('Weather bloc', () {
MockWeatherRepository mockWeatherRepository;
MockLocationRepository mockLocationRepository;
WeatherBloc weatherBloc;
setUp(() {
mockLocationRepository = MockLocationRepository();
mockWeatherRepository = MockWeatherRepository();
weatherBloc = WeatherBloc(mockWeatherRepository, mockLocationRepository);
});
tearDown(() {
resetMockitoState();
});
test('should handle fetch weather event', () {
final expectedWeatherModel = WeatherModel(20, 'Sydney', 'Sunny', Icons.cloud, []);
final mapCoordinate = MapCoordinate(1.0, -1.0);
when(mockLocationRepository.getCurrentLocation()).thenAnswer((_) {
return Future.value(mapCoordinate);
});
when(mockWeatherRepository.getWeatherWithLocation(mapCoordinate))
.thenAnswer((_) {
return Future.value(expectedWeatherModel);
});
weatherBloc.inputWeatherEvent.add(FetchWeatherEvent());
expectLater(weatherBloc.weatherModel, emits(expectedWeatherModel))
.then((_) {
verify(mockLocationRepository.getCurrentLocation());
verify(mockWeatherRepository.getWeatherWithLocation(mapCoordinate));
});
});
test('should rethrow error on get location error', () {
when(mockLocationRepository.getCurrentLocation()).thenAnswer((_) {
return Future.error('error');
});
weatherBloc.inputWeatherEvent.add(FetchWeatherEvent());
expectLater(weatherBloc.weatherModel, emitsError('error')).then((_) {
verify(mockLocationRepository.getCurrentLocation());
verifyZeroInteractions(mockWeatherRepository);
});
});
test('should rethrow error on get weather error', () {
final mapCoordinate = MapCoordinate(0, 0);
when(mockLocationRepository.getCurrentLocation()).thenAnswer((_) {
return Future.value(mapCoordinate);
});
when(mockWeatherRepository.getWeatherWithLocation(mapCoordinate))
.thenAnswer((_) {
return Future.error('error');
});
weatherBloc.inputWeatherEvent.add(FetchWeatherEvent());
expectLater(weatherBloc.weatherModel, emitsError('error')).then((_) {
verify(mockLocationRepository.getCurrentLocation());
verify(mockWeatherRepository.getWeatherWithLocation(mapCoordinate));
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment