Skip to content

Commit

Permalink
refactor(flutter_weather): use setUp in blocTest
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Oct 6, 2021
1 parent ba8e137 commit 1448f9a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
13 changes: 2 additions & 11 deletions examples/flutter_weather/test/theme/cubit/theme_cubit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,13 @@ class MockWeather extends Mock implements Weather {
void main() {
initHydratedBloc();
group('ThemeCubit', () {
late ThemeCubit themeCubit;

setUp(() {
themeCubit = ThemeCubit();
});

tearDown(() {
themeCubit.close();
});

test('initial state is correct', () {
expect(themeCubit.state, ThemeCubit.defaultColor);
expect(ThemeCubit().state, ThemeCubit.defaultColor);
});

group('toJson/fromJson', () {
test('work properly', () {
final themeCubit = ThemeCubit();
expect(
themeCubit.fromJson(themeCubit.toJson(themeCubit.state)),
themeCubit.state,
Expand Down
57 changes: 28 additions & 29 deletions examples/flutter_weather/test/weather/cubit/weather_cubit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ void main() {
group('WeatherCubit', () {
late weather_repository.Weather weather;
late weather_repository.WeatherRepository weatherRepository;
late WeatherCubit weatherCubit;

setUpAll(initHydratedBloc);

Expand All @@ -31,21 +30,19 @@ void main() {
when(() => weather.condition).thenReturn(weatherCondition);
when(() => weather.location).thenReturn(weatherLocation);
when(() => weather.temperature).thenReturn(weatherTemperature);
when(() => weatherRepository.getWeather(any()))
.thenAnswer((_) async => weather);
weatherCubit = WeatherCubit(weatherRepository);
});

tearDown(() {
weatherCubit.close();
when(
() => weatherRepository.getWeather(any()),
).thenAnswer((_) async => weather);
});

test('initial state is correct', () {
final weatherCubit = WeatherCubit(weatherRepository);
expect(weatherCubit.state, WeatherState());
});

group('toJson/fromJson', () {
test('work properly', () {
final weatherCubit = WeatherCubit(weatherRepository);
expect(
weatherCubit.fromJson(weatherCubit.toJson(weatherCubit.state)),
weatherCubit.state,
Expand All @@ -56,21 +53,21 @@ void main() {
group('fetchWeather', () {
blocTest<WeatherCubit, WeatherState>(
'emits nothing when city is null',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
act: (cubit) => cubit.fetchWeather(null),
expect: () => <WeatherState>[],
);

blocTest<WeatherCubit, WeatherState>(
'emits nothing when city is empty',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
act: (cubit) => cubit.fetchWeather(''),
expect: () => <WeatherState>[],
);

blocTest<WeatherCubit, WeatherState>(
'calls getWeather with correct city',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
act: (cubit) => cubit.fetchWeather(weatherLocation),
verify: (_) {
verify(() => weatherRepository.getWeather(weatherLocation)).called(1);
Expand All @@ -79,11 +76,12 @@ void main() {

blocTest<WeatherCubit, WeatherState>(
'emits [loading, failure] when getWeather throws',
build: () {
when(() => weatherRepository.getWeather(any()))
.thenThrow(Exception('oops'));
return weatherCubit;
setUp: () {
when(
() => weatherRepository.getWeather(any()),
).thenThrow(Exception('oops'));
},
build: () => WeatherCubit(weatherRepository),
act: (cubit) => cubit.fetchWeather(weatherLocation),
expect: () => <WeatherState>[
WeatherState(status: WeatherStatus.loading),
Expand All @@ -93,7 +91,7 @@ void main() {

blocTest<WeatherCubit, WeatherState>(
'emits [loading, success] when getWeather returns (celsius)',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
act: (cubit) => cubit.fetchWeather(weatherLocation),
expect: () => <dynamic>[
WeatherState(status: WeatherStatus.loading),
Expand All @@ -117,7 +115,7 @@ void main() {

blocTest<WeatherCubit, WeatherState>(
'emits [loading, success] when getWeather returns (fahrenheit)',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
seed: () => WeatherState(temperatureUnits: TemperatureUnits.fahrenheit),
act: (cubit) => cubit.fetchWeather(weatherLocation),
expect: () => <dynamic>[
Expand Down Expand Up @@ -147,7 +145,7 @@ void main() {
group('refreshWeather', () {
blocTest<WeatherCubit, WeatherState>(
'emits nothing when status is not success',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
act: (cubit) => cubit.refreshWeather(),
expect: () => <WeatherState>[],
verify: (_) {
Expand All @@ -157,7 +155,7 @@ void main() {

blocTest<WeatherCubit, WeatherState>(
'emits nothing when location is null',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
seed: () => WeatherState(status: WeatherStatus.success),
act: (cubit) => cubit.refreshWeather(),
expect: () => <WeatherState>[],
Expand All @@ -168,7 +166,7 @@ void main() {

blocTest<WeatherCubit, WeatherState>(
'invokes getWeather with correct location',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
seed: () => WeatherState(
status: WeatherStatus.success,
weather: Weather(
Expand All @@ -186,11 +184,12 @@ void main() {

blocTest<WeatherCubit, WeatherState>(
'emits nothing when exception is thrown',
build: () {
when(() => weatherRepository.getWeather(any()))
.thenThrow(Exception('oops'));
return weatherCubit;
setUp: () {
when(
() => weatherRepository.getWeather(any()),
).thenThrow(Exception('oops'));
},
build: () => WeatherCubit(weatherRepository),
seed: () => WeatherState(
status: WeatherStatus.success,
weather: Weather(
Expand All @@ -206,7 +205,7 @@ void main() {

blocTest<WeatherCubit, WeatherState>(
'emits updated weather (celsius)',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
seed: () => WeatherState(
status: WeatherStatus.success,
weather: Weather(
Expand Down Expand Up @@ -238,7 +237,7 @@ void main() {

blocTest<WeatherCubit, WeatherState>(
'emits updated weather (fahrenheit)',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
seed: () => WeatherState(
temperatureUnits: TemperatureUnits.fahrenheit,
status: WeatherStatus.success,
Expand Down Expand Up @@ -273,7 +272,7 @@ void main() {
group('toggleUnits', () {
blocTest<WeatherCubit, WeatherState>(
'emits updated units when status is not success',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
act: (cubit) => cubit.toggleUnits(),
expect: () => <WeatherState>[
WeatherState(temperatureUnits: TemperatureUnits.fahrenheit),
Expand All @@ -283,7 +282,7 @@ void main() {
blocTest<WeatherCubit, WeatherState>(
'emits updated units and temperature '
'when status is success (celsius)',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
seed: () => WeatherState(
status: WeatherStatus.success,
temperatureUnits: TemperatureUnits.fahrenheit,
Expand Down Expand Up @@ -312,7 +311,7 @@ void main() {
blocTest<WeatherCubit, WeatherState>(
'emits updated units and temperature '
'when status is success (fahrenheit)',
build: () => weatherCubit,
build: () => WeatherCubit(weatherRepository),
seed: () => WeatherState(
status: WeatherStatus.success,
temperatureUnits: TemperatureUnits.celsius,
Expand Down

0 comments on commit 1448f9a

Please sign in to comment.