Skip to content

Commit

Permalink
test: [#17] 회원가입 API, UseCase 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
Younggun-Kim committed Nov 23, 2024
1 parent 46d33fd commit 4127ef8
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 4 deletions.
16 changes: 15 additions & 1 deletion lib/core/utils/extensions/string_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ extension StringExt on String {

/// 날짜 형식으로 변경
DateTime toDate() {
return DateTime.parse(this);
if (length != 8) {
throw FormatException(
'Invalid date string format. Expected 8 digits (yyyyMMdd) : $this',
);
}

try {
return DateTime(
int.parse(substring(0, 4)),
int.parse(substring(4, 6)),
int.parse(substring(6, 8)),
);
} catch (e) {
throw FormatException('Failed to parse date string: $this');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ part 'sign_up_request_dto.freezed.dart';

part 'sign_up_request_dto.g.dart';

part 'sign_up_request_dto.mock.dart';

@freezed
class SignUpRequestDto with _$SignUpRequestDto {
factory SignUpRequestDto({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
part of 'sign_up_request_dto.dart';

extension SignUpRequestDtoMock on SignUpRequestDto {
static SignUpRequestDto mock() {
return SignUpRequestDto(
loginId: 'test@test.com',
password: '123qwe!@',
type: LoginType.email,
gender: GenderType.man,
name: '김땡땡',
birthDate: DateTime.now(),
cellPhoneNo: '01012345678',
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of 'sign_up_response_dto.dart';

extension SignUpResponseDtoMock on SignUpResponseDto {
static BaseResponseDto success() {
static BaseResponseDto<SignUpResponseData> success() {
return BaseResponseDtoMock.mock(
SignUpResponseData(
status: true,
Expand All @@ -16,7 +16,7 @@ extension SignUpResponseDtoMock on SignUpResponseDto {
);
}

static BaseResponseDto failure() {
static BaseResponseDto<SignUpResponseData> failure() {
return BaseResponseDtoMock.mock(
SignUpResponseData(
status: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
part of 'sign_up_request_entity.dart';

extension SignUpRequestEntityMock on SignUpRequestEntity {

static SignUpRequestEntity mock() {
return SignUpRequestEntity(
loginId: const Email(value: 'test@test.com'),
password: const Password('123qwe!@'),
loginType: LoginType.email,
gender: GenderType.man,
name: const Name('김땡땡'),
birthDate: const BirthDate('19930604'),
phone: const Phone('01012345678'),
);
}
}
83 changes: 83 additions & 0 deletions test/feature/account/data/data_source/remote/api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,87 @@ void main() {
expect(result, isA<ApiResponse<BaseResponseDto<bool>>>());
});
});

group('회원가입 API 테스트', () {
final requestDto = SignUpRequestDtoMock.mock();

test('회원가입 성공', () async {
/// Given
final responseDto = SignUpResponseDtoMock.success();
when(
mockDio.post(
api.signUpPath,
data: requestDto.toJson(),
),
).thenAnswer((_) async {
return Response(
data: responseDto.toJson(
(data) => data.toJson(),
),
statusCode: 200,
requestOptions: RequestOptions(),
);
});

/// When
final result = await api.signUp(dto: requestDto);

/// Then
expect(result, isA<ApiResponse<SignUpResponseDto>>());
expect(result.successData, isA<SignUpResponseDto>());
expect(result.successData, responseDto);
});

test('회원가입 성공', () async {
/// Given
final responseDto = SignUpResponseDtoMock.failure();

when(
mockDio.post(
api.signUpPath,
data: requestDto.toJson(),
),
).thenAnswer((_) async {
return Response(
data: responseDto.toJson(
(data) => data.toJson(),
),
statusCode: 200,
requestOptions: RequestOptions(),
);
});

/// When
final result = await api.signUp(dto: requestDto);

/// Then
expect(result, isA<ApiResponse<SignUpResponseDto>>());
expect(result.successData, isA<SignUpResponseDto>());
expect(result.successData, responseDto);
expect(result.successData?.info.status, isFalse);
});

test('서버 에러 테스트', () async {
/// Given
when(
mockDio.post(
api.signUpPath,
data: requestDto.toJson(),
),
).thenAnswer((_) async {
return Response(
data: FailResponse.error(),
statusCode: 500,
requestOptions: RequestOptions(),
);
});

/// When
final result = await api.signUp(dto: requestDto);

/// Then
expect(result, isA<ApiResponse<SignUpResponseDto>>());
expect(result.failData?.status, equals(500));
});
});
}
83 changes: 83 additions & 0 deletions test/feature/account/domain/usecase/sign_up/usecase_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:withu_app/core/core.dart';
import 'package:withu_app/feature/account/account.dart';

class MockAccountRepository extends Mock implements AccountRepository {}

void main() {
late MockAccountRepository mockRepo;
late SignUpUseCase useCase;

setUpAll(() {
registerFallbackValue(SignUpRequestDtoMock.mock());
});

setUp(() {
mockRepo = MockAccountRepository();
useCase = SignUpUseCaseImpl(accountRepo: mockRepo);
});

group('회워 가입 UseCase 테스트', () {
test('회원가입 성공', () async {
/// Given
final requestEntity = SignUpRequestEntityMock.mock();
final expectDto = SignUpResponseDtoMock.success();

when(
() => mockRepo.signUp(dto: any(named: 'dto')),
).thenAnswer(
(_) async => ApiResponse.success(expectDto),
);

/// When
final result = await useCase.exec(entity: requestEntity);

/// Then
expect(result, isA<SignUpResultEntity>());
expect(result.status, isTrue);
verify(() => mockRepo.signUp(dto: any(named: 'dto'))).called(1);
});

test('회원가입 실패', () async {
/// Given
final requestEntity = SignUpRequestEntityMock.mock();
final expectDto = SignUpResponseDtoMock.failure();

when(
() => mockRepo.signUp(dto: any(named: 'dto')),
).thenAnswer(
(_) async => ApiResponse.success(expectDto),
);

/// When
final result = await useCase.exec(entity: requestEntity);

/// Then
expect(result, isA<SignUpResultEntity>());
expect(result.status, isFalse);
verify(() => mockRepo.signUp(dto: any(named: 'dto'))).called(1);
});

test('서버에러', () async {
/// Given
final requestEntity = SignUpRequestEntityMock.mock();
final expectDto = FailResponse.error();

when(
() => mockRepo.signUp(dto: any(named: 'dto')),
).thenAnswer(
(_) async => ApiResponse.fail(expectDto),
);

/// When
final result = await useCase.exec(entity: requestEntity);

/// Then
expect(result, isA<SignUpResultEntity>());
expect(result.status, isFalse);
expect(result.message, StringRes.serverError.tr);
verify(() => mockRepo.signUp(dto: any(named: 'dto'))).called(1);
});
});
}

0 comments on commit 4127ef8

Please sign in to comment.