[Angular 마스터하기] Day 18 - 테스팅 기초, 안전한 코드 만들기
[Angular 마스터하기] Day 18 - 테스팅 기초, 안전한 코드 만들기
이제와서 시작하는 Angular 마스터하기 - Day 18 “테스트로 안정적인 코드를 작성하세요! ✅”
오늘 배울 내용
- Jasmine/Karma 소개
- 컴포넌트 테스트
- 서비스 테스트
- 기본 테스트 작성
1. 컴포넌트 테스트
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { TestBed } from '@angular/core/testing';
import { CounterComponent } from './counter.component';
describe('CounterComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CounterComponent]
});
});
it('should create', () => {
const fixture = TestBed.createComponent(CounterComponent);
const component = fixture.componentInstance;
expect(component).toBeTruthy();
});
it('should increment count', () => {
const fixture = TestBed.createComponent(CounterComponent);
const component = fixture.componentInstance;
component.increment();
expect(component.count()).toBe(1);
});
});
2. 서비스 테스트
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { TestBed } from '@angular/core/testing';
import { DataService } from './data.service';
describe('DataService', () => {
let service: DataService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(DataService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should add data', () => {
service.addData('test');
expect(service.getData()).toContain('test');
});
});
3. 테스트 실행
1
2
3
4
5
# 테스트 실행
ng test
# 단일 실행
ng test --watch=false
📝 정리
테스트 종류
- 유닛 테스트
- 통합 테스트
- E2E 테스트
체크리스트
- 기본 테스트를 작성할 수 있나요?
- 컴포넌트를 테스트할 수 있나요?
- 서비스를 테스트할 수 있나요?
📚 다음 학습
- 이전: Day 17: 성능 최적화
- 다음: Day 19: 배포하기
“테스트는 안정적인 앱의 기반입니다!” ✅
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.