Skip to content

Commit

Permalink
feat: WeatherScreenで利用する状態の更新テスト
Browse files Browse the repository at this point in the history
  • Loading branch information
yuk1ch1 committed May 16, 2024
1 parent 57b4da8 commit 1bd45e3
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions test/weather_screen_state_controller_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_training/model/weather.dart';
import 'package:flutter_training/model/weather_condition.dart';
import 'package:flutter_training/model/weather_exception.dart';
import 'package:flutter_training/model/weather_request.dart';
import 'package:flutter_training/model/weather_response.dart';
import 'package:flutter_training/presentation/screen/weather/weather_screen_state.dart';
import 'package:flutter_training/presentation/screen/weather/weather_screen_state_controller.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

import 'weather_screen_state_controller_test.mocks.dart';

@GenerateNiceMocks([MockSpec<Weather>()])
void main() {
late MockWeather mock;
late ProviderContainer container;
late WeatherScreenStateController controller;
final request = WeatherRequest(
area: 'tokyo',
date: DateTime.now(),
);

setUp(() {
mock = MockWeather();
container = ProviderContainer(
overrides: [weatherProvider.overrideWithValue(mock)],
);
controller = container.read(weatherScreenStateControllerProvider.notifier);
});

tearDown(() {
container.dispose();
});

group('WeatherScreenで利用する状態の更新テスト', () {
test('状態更新テスト_初期状態', () {
// Then
expect(
container.read(weatherScreenStateControllerProvider),
const WeatherScreenState.initial(),
);
});

test('状態更新テスト_更新成功した場合', () {
// Given
const expectedResponse = WeatherScreenState.success(
weather: WeatherResponse(
weatherCondition: WeatherCondition.cloudy,
maxTemperature: 21,
minTemperature: 7,
),
);

provideDummy<Result<WeatherResponse, AppException>>(
const Success(
WeatherResponse(
weatherCondition: WeatherCondition.cloudy,
maxTemperature: 21,
minTemperature: 7,
),
),
);

when(mock.fetch(any)).thenReturn(
const Success<WeatherResponse, AppException>(
WeatherResponse(
weatherCondition: WeatherCondition.cloudy,
maxTemperature: 21,
minTemperature: 7,
),
),
);

// When
controller.update(request);

// Then
expect(
controller.state,
expectedResponse,
);
});

test('状態更新テスト_更新失敗した場合', () {
// Given
const expectedResponse = UnknownWeather();

provideDummy<Result<WeatherResponse, AppException>>(
const Failure(
UnknownWeather(),
),
);

when(mock.fetch(any)).thenReturn(
const Failure<WeatherResponse, AppException>(
expectedResponse,
),
);

// When
controller.update(request);

// Then
expect(
controller.state,
WeatherScreenState.error(
message: expectedResponse.message,
),
);
});
});
}
55 changes: 55 additions & 0 deletions test/weather_screen_state_controller_test.mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Mocks generated by Mockito 5.4.4 from annotations
// in flutter_training/test/weather_screen_state_controller_test.dart.
// Do not manually edit this file.

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:flutter_training/model/weather.dart' as _i2;
import 'package:flutter_training/model/weather_exception.dart' as _i4;
import 'package:flutter_training/model/weather_request.dart' as _i5;
import 'package:flutter_training/model/weather_response.dart' as _i3;
import 'package:mockito/mockito.dart' as _i1;
import 'package:mockito/src/dummies.dart' as _i6;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: deprecated_member_use
// ignore_for_file: deprecated_member_use_from_same_package
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class

/// A class which mocks [Weather].
///
/// See the documentation for Mockito's code generation for more information.
class MockWeather extends _i1.Mock implements _i2.Weather {
@override
_i2.Result<_i3.WeatherResponse, _i4.AppException> fetch(
_i5.WeatherRequest? request) =>
(super.noSuchMethod(
Invocation.method(
#fetch,
[request],
),
returnValue:
_i6.dummyValue<_i2.Result<_i3.WeatherResponse, _i4.AppException>>(
this,
Invocation.method(
#fetch,
[request],
),
),
returnValueForMissingStub:
_i6.dummyValue<_i2.Result<_i3.WeatherResponse, _i4.AppException>>(
this,
Invocation.method(
#fetch,
[request],
),
),
) as _i2.Result<_i3.WeatherResponse, _i4.AppException>);
}

0 comments on commit 1bd45e3

Please sign in to comment.