728x90
반응형

 

우리가 만든 movie.service.ts를 테스트 해보자.

 

movies.service.spec.ts

import { Test, TestingModule } from '@nestjs/testing';
import { MoviesService } from './movies.service';

describe('MoviesService', () => {
  let service: MoviesService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [MoviesService],
    }).compile();

    service = module.get<MoviesService>(MoviesService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
});

 

먼저 describe 부분은 테스트를 묘사한다는 뜻이며, beforeEach 부분은 테스트를 하기 전에 실행되는 것이다.

 

하단에 it 부분이 테스트를 실행하는 부분인데, 여기서 추가해서 테스트를 진행할 수 있다.

코드를 추가해보자.

 

movies.service.spec.ts

import { Test, TestingModule } from '@nestjs/testing';
import { MoviesService } from './movies.service';

describe('MoviesService', () => {
  let service: MoviesService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [MoviesService],
    }).compile();

    service = module.get<MoviesService>(MoviesService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });

  it("should be 4", () => {
    expect(2+2).toEqual(4)
  })
});

 

expect 에 조건을 넣고 2+2 가 toEqual안에 들어있는 4와 같으면 테스트가 통과한다.

테스트를 진행해보면,

 

 

이렇게 결과를 얻을 수 있다.

 

만약, toEqual 부분에 숫자 5를 넣고 돌려보면,

it("should be 4", () => {
    expect(2+2).toEqual(5)
  })

이렇게 실패했다는 화면을 볼 수 있다. 왜 실패했는지도 말해준다.

728x90
반응형

+ Recent posts