tdd 진행
This commit is contained in:
@@ -0,0 +1,301 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:lanebow/services/club_service.dart';
|
||||
import 'package:lanebow/services/api_service.dart';
|
||||
import 'package:lanebow/services/event_bus.dart';
|
||||
import 'package:lanebow/models/club_model.dart';
|
||||
import 'package:lanebow/config/api_config.dart';
|
||||
|
||||
// 모킹 클래스 생성
|
||||
@GenerateMocks([ApiService])
|
||||
import 'club_service_test.mocks.dart';
|
||||
|
||||
void main() {
|
||||
late ClubService clubService;
|
||||
late MockApiService mockApiService;
|
||||
final testToken = 'test_token';
|
||||
final testClubId = 'test_club_id';
|
||||
|
||||
// 테스트 클럽 데이터
|
||||
final testClub = {
|
||||
'id': 'test_club_id',
|
||||
'name': '테스트 클럽',
|
||||
'description': '테스트 클럽 설명',
|
||||
'location': '테스트 위치',
|
||||
'ownerId': 'test_owner_id',
|
||||
'createdAt': DateTime.now().toIso8601String(),
|
||||
'updatedAt': DateTime.now().toIso8601String(),
|
||||
'memberCount': 10,
|
||||
'isActive': true,
|
||||
};
|
||||
|
||||
setUp(() async {
|
||||
// SharedPreferences 모킹
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
|
||||
// API 서비스 모킹
|
||||
mockApiService = MockApiService();
|
||||
|
||||
// ClubService 초기화 (테스트용 생성자 사용)
|
||||
clubService = ClubService.forTest(mockApiService);
|
||||
|
||||
// 토큰 설정
|
||||
await clubService.initialize(testToken);
|
||||
});
|
||||
|
||||
group('ClubService 테스트', () {
|
||||
test('initialize 메서드가 토큰을 설정해야 함', () async {
|
||||
// given
|
||||
// setUp에서 이미 initialize 호출됨
|
||||
|
||||
// then
|
||||
verify(mockApiService.setToken(testToken)).called(1);
|
||||
});
|
||||
|
||||
test('initialize 메서드가 저장된 클럽 ID가 있으면 클럽 정보를 가져와야 함', () async {
|
||||
// given
|
||||
SharedPreferences.setMockInitialValues({
|
||||
ApiConfig.clubIdKey: testClubId,
|
||||
});
|
||||
|
||||
when(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: {'clubId': testClubId},
|
||||
)).thenAnswer((_) async => testClub);
|
||||
|
||||
// when
|
||||
await clubService.initialize(testToken);
|
||||
|
||||
// then
|
||||
verify(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: {'clubId': testClubId},
|
||||
)).called(1);
|
||||
|
||||
expect(clubService.currentClub?.id, testClubId);
|
||||
expect(clubService.currentClub?.name, '테스트 클럽');
|
||||
});
|
||||
|
||||
test('fetchUserClubs 메서드가 사용자의 클럽 목록을 가져와야 함', () async {
|
||||
// given
|
||||
final testClubs = [testClub];
|
||||
when(mockApiService.post('${ApiConfig.clubs}/user'))
|
||||
.thenAnswer((_) async => testClubs);
|
||||
|
||||
// when
|
||||
await clubService.fetchUserClubs();
|
||||
|
||||
// then
|
||||
verify(mockApiService.post('${ApiConfig.clubs}/user')).called(1);
|
||||
|
||||
expect(clubService.clubs.length, 1);
|
||||
expect(clubService.clubs[0].id, testClubId);
|
||||
expect(clubService.clubs[0].name, '테스트 클럽');
|
||||
});
|
||||
|
||||
test('fetchClubById 메서드가 특정 클럽 정보를 가져와야 함', () async {
|
||||
// given
|
||||
when(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: {'clubId': testClubId},
|
||||
)).thenAnswer((_) async => testClub);
|
||||
|
||||
// when
|
||||
await clubService.fetchClubById(testClubId);
|
||||
|
||||
// then
|
||||
verify(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: {'clubId': testClubId},
|
||||
)).called(1);
|
||||
|
||||
expect(clubService.currentClub?.id, testClubId);
|
||||
expect(clubService.currentClub?.name, '테스트 클럽');
|
||||
});
|
||||
|
||||
test('setCurrentClub 메서드가 클럽을 설정해야 함', () async {
|
||||
// given
|
||||
when(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: {'clubId': testClubId},
|
||||
)).thenAnswer((_) async => testClub);
|
||||
|
||||
// when
|
||||
await clubService.setCurrentClub(testClubId);
|
||||
|
||||
// then
|
||||
verify(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: {'clubId': testClubId},
|
||||
)).called(1);
|
||||
|
||||
expect(clubService.currentClub?.id, testClubId);
|
||||
});
|
||||
|
||||
test('selectClub 메서드가 클럽을 선택하고 이벤트를 발생시켜야 함', () async {
|
||||
// given
|
||||
when(mockApiService.post(
|
||||
'${ApiConfig.clubs}/select-club',
|
||||
data: {'clubId': testClubId},
|
||||
)).thenAnswer((_) async => {});
|
||||
|
||||
when(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: {'clubId': testClubId},
|
||||
)).thenAnswer((_) async => testClub);
|
||||
|
||||
// 이벤트 버스 테스트를 위한 변수
|
||||
bool eventFired = false;
|
||||
String? eventClubId;
|
||||
|
||||
// 이벤트 리스너 등록 - 리스너를 변수에 저장하여 나중에 확인 가능하게 함
|
||||
final subscription = EventBus().on<ClubChangedEvent>().listen((event) {
|
||||
eventFired = true;
|
||||
eventClubId = event.clubId;
|
||||
});
|
||||
|
||||
// when
|
||||
await clubService.selectClub(testClubId);
|
||||
|
||||
// 이벤트 처리를 위한 지연 추가
|
||||
await Future.delayed(Duration(milliseconds: 100));
|
||||
|
||||
// then
|
||||
verify(mockApiService.post(
|
||||
'${ApiConfig.clubs}/select-club',
|
||||
data: {'clubId': testClubId},
|
||||
)).called(1);
|
||||
|
||||
verify(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: {'clubId': testClubId},
|
||||
)).called(1);
|
||||
|
||||
expect(clubService.currentClub?.id, testClubId);
|
||||
|
||||
// 이벤트가 발생했는지 확인
|
||||
expect(eventFired, true);
|
||||
expect(eventClubId, testClubId);
|
||||
|
||||
// 구독 취소
|
||||
subscription.cancel();
|
||||
});
|
||||
|
||||
test('createClub 메서드가 새 클럽을 생성해야 함', () async {
|
||||
// given
|
||||
final newClub = Club(
|
||||
name: '새 클럽',
|
||||
description: '새 클럽 설명',
|
||||
location: '새 위치',
|
||||
);
|
||||
|
||||
when(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: newClub.toJson(),
|
||||
)).thenAnswer((_) async => {'club': testClub});
|
||||
|
||||
// when
|
||||
final result = await clubService.createClub(newClub);
|
||||
|
||||
// then
|
||||
verify(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: newClub.toJson(),
|
||||
)).called(1);
|
||||
|
||||
expect(result.id, testClubId);
|
||||
expect(clubService.clubs.length, 1);
|
||||
expect(clubService.currentClub?.id, testClubId);
|
||||
});
|
||||
|
||||
test('updateClub 메서드가 클럽 정보를 업데이트해야 함', () async {
|
||||
// given
|
||||
final updates = {'name': '업데이트된 클럽'};
|
||||
final updatedClub = Map<String, dynamic>.from(testClub);
|
||||
updatedClub['name'] = '업데이트된 클럽';
|
||||
|
||||
// 클럽 목록에 테스트 클럽 추가
|
||||
when(mockApiService.post('${ApiConfig.clubs}/user'))
|
||||
.thenAnswer((_) async => [testClub]);
|
||||
await clubService.fetchUserClubs();
|
||||
|
||||
// 현재 클럽 설정
|
||||
when(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: {'clubId': testClubId},
|
||||
)).thenAnswer((_) async => testClub);
|
||||
await clubService.fetchClubById(testClubId);
|
||||
|
||||
when(mockApiService.put(
|
||||
ApiConfig.clubs,
|
||||
data: {
|
||||
'clubId': testClubId,
|
||||
...updates,
|
||||
},
|
||||
)).thenAnswer((_) async => {'club': updatedClub});
|
||||
|
||||
// when
|
||||
final result = await clubService.updateClub(testClubId, updates);
|
||||
|
||||
// then
|
||||
verify(mockApiService.put(
|
||||
ApiConfig.clubs,
|
||||
data: {
|
||||
'clubId': testClubId,
|
||||
...updates,
|
||||
},
|
||||
)).called(1);
|
||||
|
||||
expect(result.name, '업데이트된 클럽');
|
||||
expect(clubService.clubs[0].name, '업데이트된 클럽');
|
||||
expect(clubService.currentClub?.name, '업데이트된 클럽');
|
||||
});
|
||||
|
||||
test('updateClub 메서드가 직접 클럽 객체가 반환되는 경우도 처리해야 함', () async {
|
||||
// given
|
||||
final updates = {'name': '업데이트된 클럽'};
|
||||
final updatedClub = Map<String, dynamic>.from(testClub);
|
||||
updatedClub['name'] = '업데이트된 클럽';
|
||||
|
||||
// 클럽 목록에 테스트 클럽 추가
|
||||
when(mockApiService.post('${ApiConfig.clubs}/user'))
|
||||
.thenAnswer((_) async => [testClub]);
|
||||
await clubService.fetchUserClubs();
|
||||
|
||||
// 현재 클럽 설정
|
||||
when(mockApiService.post(
|
||||
'${ApiConfig.clubs}',
|
||||
data: {'clubId': testClubId},
|
||||
)).thenAnswer((_) async => testClub);
|
||||
await clubService.fetchClubById(testClubId);
|
||||
|
||||
// 직접 클럽 객체 반환 시뮬레이션
|
||||
when(mockApiService.put(
|
||||
ApiConfig.clubs,
|
||||
data: {
|
||||
'clubId': testClubId,
|
||||
...updates,
|
||||
},
|
||||
)).thenAnswer((_) async => updatedClub);
|
||||
|
||||
// when
|
||||
final result = await clubService.updateClub(testClubId, updates);
|
||||
|
||||
// then
|
||||
verify(mockApiService.put(
|
||||
ApiConfig.clubs,
|
||||
data: {
|
||||
'clubId': testClubId,
|
||||
...updates,
|
||||
},
|
||||
)).called(1);
|
||||
|
||||
expect(result.name, '업데이트된 클럽');
|
||||
expect(clubService.clubs[0].name, '업데이트된 클럽');
|
||||
expect(clubService.currentClub?.name, '업데이트된 클럽');
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user