TDD 작성
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:async';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
@@ -8,6 +9,7 @@ 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';
|
||||
import 'package:lanebow/utils/test_utils.dart';
|
||||
|
||||
// 모킹 클래스 생성
|
||||
@GenerateMocks([ApiService])
|
||||
@@ -33,6 +35,13 @@ void main() {
|
||||
};
|
||||
|
||||
setUp(() async {
|
||||
// 테스트 모드 강제 설정
|
||||
TestUtils.setTestMode(true);
|
||||
|
||||
// 테스트별 고유 ID 생성 및 설정 - 일관된 패턴 적용
|
||||
final testId = 'club_service_test_${DateTime.now().millisecondsSinceEpoch}';
|
||||
EventBus.setCurrentTestId(testId); // 이 호출로 모든 인스턴스가 초기화됨
|
||||
|
||||
// SharedPreferences 모킹
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
|
||||
@@ -42,9 +51,31 @@ void main() {
|
||||
// ClubService 초기화 (테스트용 생성자 사용)
|
||||
clubService = ClubService.forTest(mockApiService);
|
||||
|
||||
// 토큰 설정
|
||||
// 테스트용 토큰 초기화
|
||||
await clubService.initialize(testToken);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
// 테스트 종료 후 정리 작업
|
||||
try {
|
||||
// 서비스 정리 (한 번만 호출)
|
||||
clubService.dispose();
|
||||
} catch (e) {
|
||||
// dispose 중 오류 무시
|
||||
if (kDebugMode) {
|
||||
print('ClubService dispose 중 오류 무시: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// EventBus 테스트 ID 초기화 - 일관된 패턴 적용
|
||||
EventBus.clearCurrentTestId(); // 이 호출로 모든 인스턴스가 초기화됨
|
||||
|
||||
// 테스트 모드 해제
|
||||
TestUtils.setTestMode(false);
|
||||
|
||||
// 추가 지연으로 비동기 작업 완료 대기
|
||||
await Future.delayed(Duration(milliseconds: 100));
|
||||
});
|
||||
|
||||
group('ClubService 테스트', () {
|
||||
test('initialize 메서드가 토큰을 설정해야 함', () async {
|
||||
@@ -150,38 +181,41 @@ void main() {
|
||||
// 이벤트 버스 테스트를 위한 변수
|
||||
bool eventFired = false;
|
||||
String? eventClubId;
|
||||
StreamSubscription? subscription;
|
||||
|
||||
// 이벤트 리스너 등록 - 리스너를 변수에 저장하여 나중에 확인 가능하게 함
|
||||
final subscription = EventBus().on<ClubChangedEvent>().listen((event) {
|
||||
eventFired = true;
|
||||
eventClubId = event.clubId;
|
||||
});
|
||||
try {
|
||||
// 이벤트 리스너 등록 - 리스너를 변수에 저장하여 나중에 확인 가능하게 함
|
||||
subscription = EventBus().on<ClubChangedEvent>().listen((event) {
|
||||
eventFired = true;
|
||||
eventClubId = event.clubId;
|
||||
});
|
||||
|
||||
// when
|
||||
await clubService.selectClub(testClubId);
|
||||
// when
|
||||
await clubService.selectClub(testClubId);
|
||||
|
||||
// 이벤트 처리를 위한 지연 추가
|
||||
await Future.delayed(Duration(milliseconds: 100));
|
||||
// 이벤트 처리를 위한 지연 추가 - 시간 증가
|
||||
await Future.delayed(Duration(milliseconds: 500));
|
||||
|
||||
// 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();
|
||||
// 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);
|
||||
} finally {
|
||||
// 구독 취소 - 반드시 실행되도록 finally 블록에 배치
|
||||
await subscription?.cancel();
|
||||
}
|
||||
});
|
||||
|
||||
test('createClub 메서드가 새 클럽을 생성해야 함', () async {
|
||||
|
||||
Reference in New Issue
Block a user