Skip to content

Commit

Permalink
Merge branch 'main' into session/10
Browse files Browse the repository at this point in the history
  • Loading branch information
mqkotoo authored Oct 12, 2023
2 parents 538026c + 4e41359 commit eba4515
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 46 deletions.
43 changes: 8 additions & 35 deletions test/model/weather_forecast_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ void main() {
final data = jsonDecode(validJsonData) as Map<String, dynamic>;
final result = WeatherForecast.fromJson(data);

expect(result.weatherCondition, WeatherCondition.cloudy);
expect(result.maxTemperature, 25);
expect(result.minTemperature, 7);
expect(
result.date,
DateTime(2023, 9, 19, 10, 24, 31, 877),
result,
WeatherForecast(
weatherCondition: WeatherCondition.cloudy,
maxTemperature: 25,
minTemperature: 7,
date: DateTime(2023, 9, 19, 10, 24, 31, 877),
),
);
});

group('failure case: fromJon', () {
group('fromJson failure: CheckedFromJsonException should be thrown.', () {
test('non-exist weather', () {
const jsonData = '''
{
Expand Down Expand Up @@ -96,33 +98,4 @@ void main() {
);
});
});

// jsonのデコードのテストも以下に書く
test('jsonDecode() success case', () {
const jsonData = '''
{
"weather_condition": "cloudy",
"max_temperature": 25,
"min_temperature": 7,
"date": "2023-09-19 10:24:31.877"
}
''';

final decodedData = jsonDecode(jsonData) as Map<String, dynamic>;

expect(decodedData, isA<Map<String, dynamic>>());
expect(decodedData['weather_condition'], 'cloudy');
expect(decodedData['max_temperature'], 25);
expect(decodedData['min_temperature'], 7);
expect(decodedData['date'], '2023-09-19 10:24:31.877');
});

test('jsonDecode() failure case', () {
const jsonData = '{invalid json data}';

expect(
() => jsonDecode(jsonData) as Map<String, dynamic>,
throwsA(isA<FormatException>()),
);
});
}
8 changes: 4 additions & 4 deletions test/service/weather_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ void main() {
final mockClient = MockYumemiWeather();
late ProviderContainer container;

setUpAll(() {
setUp(() {
//mockのYumemiWeatherでDIする
container = ProviderContainer(
overrides: [yumemiWeatherClientProvider.overrideWithValue(mockClient)],
);
});

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

Expand All @@ -55,7 +55,7 @@ void main() {
});

group('failure case', () {
test('invalidParameter error', () {
test('invalidParameter error is thrown', () {
when(mockClient.fetchWeather(any))
.thenThrow(YumemiWeatherError.invalidParameter);

Expand All @@ -72,7 +72,7 @@ void main() {
);
});

test('unknown error', () {
test('unknown error is thrown', () {
when(mockClient.fetchWeather(any)).thenThrow(YumemiWeatherError.unknown);

final result =
Expand Down
12 changes: 5 additions & 7 deletions test/state/weather_state_notifier_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ void main() {
final mockClient = MockYumemiWeather();
late ProviderContainer container;

setUpAll(() {
setUp(() {
//mockのYumemiWeatherでDIする
container = ProviderContainer(
overrides: [yumemiWeatherClientProvider.overrideWithValue(mockClient)],
);
});

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

Expand All @@ -44,8 +44,6 @@ void main() {

final weatherState = container.read(weatherStateNotifierProvider);

expect(weatherState, isNotNull);

expect(
weatherState,
WeatherForecast(
Expand All @@ -58,7 +56,7 @@ void main() {
});

group('failure case: getWeather()', () {
test('unknown error case', () {
test('an unknown error is thrown', () {
when(mockClient.fetchWeather(any)).thenThrow(YumemiWeatherError.unknown);

// 表示されるエラーメッセージを格納
Expand All @@ -79,7 +77,7 @@ void main() {
expect(errorMessage, ErrorMessage.unknown);
});

test('invalidParameter error case', () {
test('invalidParameter error is thrown', () {
when(mockClient.fetchWeather(any))
.thenThrow(YumemiWeatherError.invalidParameter);

Expand Down Expand Up @@ -123,7 +121,7 @@ void main() {
expect(errorMessage, ErrorMessage.receiveInvalidData);
});

test('jsonDecode() error case', () {
test('received data is not in JSON format', () {
when(mockClient.fetchWeather(any))
.thenReturn(invalidJsonDataForFormatException);

Expand Down

0 comments on commit eba4515

Please sign in to comment.