이벤트 가져오기 위젯 테스트 추가 및 테스트 훅 도입

This commit is contained in:
2025-09-12 06:33:44 +09:00
parent 107abc963f
commit a5f2544d11
121 changed files with 39277 additions and 3911 deletions
@@ -22,21 +22,23 @@ class MockPurchaseDetails implements PurchaseDetails {
required this.verificationData,
required PurchaseStatus status,
}) : _status = status;
@override
final String purchaseID;
@override
final String productID;
@override
final PurchaseVerificationData verificationData;
// status 구현
PurchaseStatus _status;
@override
PurchaseStatus get status => _status;
@override
set status(PurchaseStatus value) { _status = value; }
set status(PurchaseStatus value) {
_status = value;
}
@override
IAPError? error;
@override
@@ -61,111 +63,121 @@ class MockPurchaseVerificationData implements PurchaseVerificationData {
class CustomMockInAppPurchaseService implements InAppPurchaseService {
// 내부 상태 변수
List<ProductDetails> _products = [];
List<PurchaseDetails> _purchases = [];
final List<PurchaseDetails> _purchases = [];
bool _isAvailable = true;
bool _isLoading = false;
String? _error;
bool _isPurchaseVerified = false;
PurchaseDetails? _lastVerifiedPurchase;
// 필수 속성 구현 - 게터
@override
List<ProductDetails> get products => _products;
@override
List<PurchaseDetails> get purchases => _purchases;
@override
bool get isAvailable => _isAvailable;
@override
bool get isLoading => _isLoading;
@override
String? get error => _error;
@override
bool get isPurchaseVerified => _isPurchaseVerified;
@override
PurchaseDetails? get lastVerifiedPurchase => _lastVerifiedPurchase;
// 필수 속성 구현 - 세터
@override
set isPurchaseVerified(bool value) => _isPurchaseVerified = value;
@override
set lastVerifiedPurchase(PurchaseDetails? value) {
_lastVerifiedPurchase = value;
if (value != null && !_purchases.contains(value)) {
_purchases.add(value);
}
}
// 필수 메서드 구현
@override
Future<bool> initialize() async {
_isLoading = true;
notifyListeners();
_isAvailable = true;
_isLoading = false;
notifyListeners();
return true;
}
@override
Future<bool> purchaseSubscription(SubscriptionPlanType planType) async {
return true;
}
@override
Future<bool> restorePurchases() async {
return true;
}
@override
ProductDetails? getProductByPlanType(SubscriptionPlanType planType) {
// 테스트용 간단 구현
return _products.isNotEmpty ? _products.first : null;
}
@override
void dispose() {
// 아무것도 하지 않음 - 테스트용
}
// ChangeNotifier 구현
final List<VoidCallback> _listeners = [];
@override
void addListener(VoidCallback listener) {
_listeners.add(listener);
}
@override
void removeListener(VoidCallback listener) {
_listeners.remove(listener);
}
@override
void notifyListeners() {
for (final listener in _listeners) {
listener();
}
}
@override
bool get hasListeners => _listeners.isNotEmpty;
// 테스트용 설정 메서드
void setIsPurchaseVerified(bool value) {
_isPurchaseVerified = value;
}
void setLastVerifiedPurchase(PurchaseDetails? purchase) {
_lastVerifiedPurchase = purchase;
}
void setProducts(List<ProductDetails> products) {
_products = products;
}
void setError(String? error) {
_error = error;
}
void setIsAvailable(bool value) {
_isAvailable = value;
}
@@ -192,16 +204,15 @@ void main() {
'status': 'active',
'startDate': DateTime.now().toIso8601String(),
'endDate': DateTime.now().add(Duration(days: 30)).toIso8601String(),
'price': 19900, // double이 아닌 int로 변경 (toDouble() 메서드 호출 문제 해결)
'price': 19900, // double이 아닌 int로 변경 (toDouble() 메서드 호출 문제 해결)
'currency': 'KRW',
'autoRenew': true,
'features': {
'maxMembers': 100,
'maxEvents': 100,
},
'features': {'maxMembers': 100, 'maxEvents': 100},
'paymentMethod': 'card',
'transactionId': 'test_transaction_id',
'createdAt': DateTime.now().subtract(Duration(days: 10)).toIso8601String(),
'createdAt': DateTime.now()
.subtract(Duration(days: 10))
.toIso8601String(),
'updatedAt': DateTime.now().toIso8601String(),
};
@@ -269,17 +280,21 @@ void main() {
mockPurchaseService = CustomMockInAppPurchaseService();
// 현재 구독 정보 API 응답 설정
when(mockClient.post(
Uri.parse('https://api.example.com/api/subscriptions/current'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
when(
mockClient.post(
Uri.parse('https://api.example.com/api/subscriptions/current'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
// 구독 플랜 목록 API 응답 설정
when(mockClient.get(
Uri.parse('https://api.example.com/api/subscriptions/plans'),
headers: anyNamed('headers'),
)).thenAnswer((_) async => http.Response(testSubscriptionPlansJson, 200));
when(
mockClient.get(
Uri.parse('https://api.example.com/api/subscriptions/plans'),
headers: anyNamed('headers'),
),
).thenAnswer((_) async => http.Response(testSubscriptionPlansJson, 200));
// 인앱 구매 서비스 초기화 설정 - when() 대신 직접 메서드 호출 준비
// CustomMockInAppPurchaseService는 직접 구현된 클래스이므로 when() 대신 직접 설정 사용
@@ -304,218 +319,269 @@ void main() {
// then
// loadCurrentSubscription의 반환값을 사용하지 않고 상태를 확인
expect(subscriptionService.currentSubscription, isNotNull);
expect(subscriptionService.currentSubscription!.id, 'test_subscription_id');
expect(
subscriptionService.currentSubscription!.id,
'test_subscription_id',
);
expect(subscriptionService.isLoading, isFalse);
expect(subscriptionService.error, isNull);
verify(mockClient.post(
Uri.parse('https://api.example.com/api/subscriptions/current'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).called(1);
verify(
mockClient.post(
Uri.parse('https://api.example.com/api/subscriptions/current'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).called(1);
});
test('구독 플랜 목록 로드 테스트', () async {
// given
// 이미 setUp에서 구독 플랜 목록 API 응답이 설정되어 있음
// when
// 서비스가 생성될 때 _loadAvailablePlans()가 호출되어 이미 플랜 목록이 로드되어 있음
// then
expect(subscriptionService.availablePlans, isNotEmpty);
// 테스트 플랜 수와 일치하는지 확인 (실제 플랜 수가 4개로 확인됨)
// verify를 사용하여 HTTP 요청 확인
verify(mockClient.get(
Uri.parse('https://api.example.com/api/subscriptions/plans'),
headers: anyNamed('headers'),
)).called(1);
verify(
mockClient.get(
Uri.parse('https://api.example.com/api/subscriptions/plans'),
headers: anyNamed('headers'),
),
).called(1);
});
test('구독 복원 테스트', () async {
// given
when(mockPurchaseService.restorePurchases()).thenAnswer((_) async => true);
when(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
when(
mockPurchaseService.restorePurchases(),
).thenAnswer((_) async => true);
when(
mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
// when
final result = await subscriptionService.restoreSubscriptions();
// then
expect(result, isTrue);
expect(subscriptionService.isLoading, isFalse);
verify(mockPurchaseService.restorePurchases()).called(1);
});
test('구독 상태 검증 테스트', () async {
// given
// 현재 구독 정보 설정
when(mockClient.post(
any,
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
when(
mockClient.post(
any,
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
await subscriptionService.loadCurrentSubscription();
// SubscriptionService 내부 구현에 맞게 목 설정
// verifySubscription 메서드는 GET 요청을 사용하여 구독 상태를 확인
when(mockClient.get(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
)).thenAnswer((_) async => http.Response(jsonEncode(testSubscription), 200));
when(
mockClient.get(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
),
).thenAnswer(
(_) async => http.Response(jsonEncode(testSubscription), 200),
);
// when
final result = await subscriptionService.verifySubscription();
// then
expect(result, isTrue);
expect(subscriptionService.error, isNull);
verify(mockClient.get(
any,
headers: anyNamed('headers'),
)).called(1);
verify(mockClient.get(any, headers: anyNamed('headers'))).called(1);
});
test('새 구독 생성 테스트', () async {
// given
// 인앱 구매 서비스 목 설정
when(mockPurchaseService.purchaseSubscription(SubscriptionPlanType.premium))
.thenAnswer((_) async => true);
when(
mockPurchaseService.purchaseSubscription(SubscriptionPlanType.premium),
).thenAnswer((_) async => true);
when(mockPurchaseService.isPurchaseVerified).thenReturn(true);
when(mockPurchaseService.lastVerifiedPurchase).thenReturn(
MockPurchaseDetails(
purchaseID: 'test_purchase_id',
productID: 'test_product_id',
verificationData: MockPurchaseVerificationData('test_verification_data'),
verificationData: MockPurchaseVerificationData(
'test_verification_data',
),
status: PurchaseStatus.purchased,
),
);
// 서버 API 목 설정 - 정확한 API 경로와 응답 구조 사용
when(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.subscribeClub}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response(jsonEncode(testSubscription), 200));
when(
mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.subscribeClub}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer(
(_) async => http.Response(jsonEncode(testSubscription), 200),
);
// when
final result = await subscriptionService.createSubscription(
SubscriptionPlanType.premium,
false,
);
// then
expect(result, isTrue);
verify(mockPurchaseService.purchaseSubscription(SubscriptionPlanType.premium)).called(1);
verify(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.subscribeClub}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).called(1);
verify(
mockPurchaseService.purchaseSubscription(SubscriptionPlanType.premium),
).called(1);
verify(
mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.subscribeClub}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).called(1);
});
test('구독 취소 테스트', () async {
// given
// 현재 구독 정보 설정
when(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
when(
mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
await subscriptionService.loadCurrentSubscription();
// 서버 API 목 설정
when(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.subscriptions}/${testSubscription['id']}/cancel'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response(jsonEncode(testSubscription), 200));
when(
mockClient.post(
Uri.parse(
'${ApiConfig.baseUrl}${ApiConfig.subscriptions}/${testSubscription['id']}/cancel',
),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer(
(_) async => http.Response(jsonEncode(testSubscription), 200),
);
// when
final result = await subscriptionService.cancelSubscription();
// then
expect(result, isTrue);
verify(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.subscriptions}/${testSubscription['id']}/cancel'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).called(1);
verify(
mockClient.post(
Uri.parse(
'${ApiConfig.baseUrl}${ApiConfig.subscriptions}/${testSubscription['id']}/cancel',
),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).called(1);
});
test('구독 플랜 변경 테스트', () async {
// given
// 현재 구독 정보 설정
when(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
when(
mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
await subscriptionService.loadCurrentSubscription();
// 서버 API 목 설정
when(mockClient.post(
Uri.parse('https://api.example.com/api/subscriptions/change-plan'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
when(
mockClient.post(
Uri.parse('https://api.example.com/api/subscriptions/change-plan'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
// when
final result = await subscriptionService.changePlan(
SubscriptionPlanType.premium,
true,
);
// then
expect(result, isTrue);
verify(mockClient.post(
Uri.parse('https://api.example.com/api/subscriptions/change-plan'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).called(1);
verify(
mockClient.post(
Uri.parse('https://api.example.com/api/subscriptions/change-plan'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).called(1);
});
test('자동 갱신 설정 변경 테스트', () async {
// given
// 현재 구독 정보 설정
when(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
when(
mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer((_) async => http.Response(testSubscriptionJson, 200));
await subscriptionService.loadCurrentSubscription();
// 서버 API 목 설정
when(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.subscriptions}/autorenew'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response(jsonEncode(testSubscription), 200));
when(
mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.subscriptions}/autorenew'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer(
(_) async => http.Response(jsonEncode(testSubscription), 200),
);
// when
final result = await subscriptionService.toggleAutoRenew();
// then
expect(result, isTrue);
verify(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.subscriptions}/autorenew'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).called(1);
verify(
mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.subscriptions}/autorenew'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).called(1);
});
});
@@ -585,11 +651,13 @@ void main() {
test('구독 정보 로드 실패 테스트', () async {
// given
when(mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
)).thenAnswer((_) async => http.Response('{"error": "Not found"}', 404));
when(
mockClient.post(
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.currentSubscription}'),
headers: anyNamed('headers'),
body: anyNamed('body'),
),
).thenAnswer((_) async => http.Response('{"error": "Not found"}', 404));
// when
await subscriptionService.loadCurrentSubscription();