단위 테스트

Unit Testing 유닛 테스트 과정 Fake UsersService를 만들어서 실행할 계획이다. 정상적으로 애플리케이션을 실행하면 DI 안에 많은 종속성을 넣어야 한다. 우린 새롭게 테스트를 위한 DI를 만드는데, 내부에는 Userss Service의 모든 메서드를 실행하는 클래스를 담는다. 이로서 어떤 단위(ex. Authentication, sign in 등)을 테스트하는데 종속성 주입에서 자유로워질 수 있다. Test 설정하기 users 디렉토리에 auth.service.spec.ts 파일을 만들자 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // auth.service.spec.ts import { Test } from '@nestjs/testing'; import { AuthService } from './auth.service'; import { UsersService } from './users.service'; it('can create an instance of auth service', async () => { // 새 DI container 생성 // 하지만 AUthService를 위한 종속성을 제공하지 않았으니 실행하면 오류가 뜰것이다. constmudle = await Testing.createTestingModule({ providers:[AuthService] }).compile(); const service = module.get(AuthService); expect(service).toBeDefined(); }); 터미널에서 npm run test:watch를 입력한다. 3개가 failed라고 뜰텐데, p를 누르고 auth.service.spec 을 입력한다. 그럼 이 파일만 테스트하여 1 failed라고 뜬다. ...

2023년 10월 9일 · 5 분 · 배준수