4. Create 에러 처리하기

우동이·2022년 1월 9일
0

NodeServer Test

목록 보기
5/6
post-thumbnail

4. Create 에러 처리하기

  • 에러를 생성하기 위해 Postman을 활용합니다.

단위테스트에서 async / await 적용하기

  • 단위 테스트 수정
    • productModel.save 메소드 검증 추가
it("should call ProductModel", async () => {
    // createProduct() 함수가 실행 될 때
    await createProduct(req, res, next);
    // 내부에서 new Product()가 실행 되는지
    // 여기서 단위테스트 이기 때문에 실제 Model에 영향을 받으면 안되기 떄문에 Mock 함수 사용
    // toBeCalledWith 함수를 통해 어떤 인자를 받았는지도 함께 검증
    expect(Product).toBeCalledWith(newProduct);

    // productModel.save 메소드 검증
    const productModel = new Product(newProduct);
    expect(productModel.save).toBeCalled();
  });

  it("should return 201 response code", async () => {
    await createProduct(req, res, next);
    expect(res.statusCode).toBe(201);
    // res.send() Test
    expect(res._isEndCalled()).toBeTruthy();
  });

  it("should return json body in response", async () => {
    const productModel = new Product(newProduct);
    // mock 함수에 리턴값을 req.body 값으로 설정
    (productModel.save as jest.Mock).mockReturnValue(newProduct);

    await createProduct(req, res, next);
    expect(res._getJSONData()).toStrictEqual(newProduct);
  });

에러 처리를 위한 단위 테스트 작성

  • 에러 발생시키는 방법

    • Model에서 정의한 필수 프로퍼티를 받지 못했을 때 서버에서 에러 발생
      • Model에서 특정 프로퍼티에 required: true로 설정한 경우 해당 프로퍼티를 포함해서 디비에 저장해야 합니다.
      • 만약 포함하지 않는다면 몽구스가 에러를 발생시킵니다.
  • 몽고 디비에서 처리하는 부분은 문제가 없다는 것을 가정하고 단위 테스트를 진행하기 때문에 몽고 디비에서 발생시키는 에러 부분은 Mock 함수를 통해 처리합니다.

  • 단위 테스트 작성

    • 앞서 생성한 next=null값을 jest.fn()을 통해 mock 함수로 만들어 줍니다.
import { createProduct } from "../../src/controllers/products";
import { Product } from "../../src/models/product";
import httpMocks from "node-mocks-http";
import newProduct from "../data/new-product.json";

// Model mock 함수
// 어떤 것에 의해서 호출되었는지 / 어떤 것과 함께 호출되는지 알 수 있습니다. ( 스파이 역할 )
jest.mock("../../src/models/product");

let req: any, res: any, next: any;
// beforeEach() 때문에 모든 테스트 케이스에 똑같이 적용
beforeEach(() => {
  // express controller가 받을 수 있는 인자 생성
  req = httpMocks.createRequest();
  res = httpMocks.createResponse();
  next = jest.fn();
});

it("should handle errors", async () => {
    // description 프로퍼티가 없을 때 발생하는 에러 메시지 정의
    const errorMessage = { message: "description property missing" };
    // 비동기 처리에서 에러 프로미스 정의
    const rejectedPromise = Promise.reject(errorMessage);
    const productModel = new Product(newProduct);

    // mock 함수의 리턴 값을 정의 => rejectedPromise 반환
    (productModel.save as jest.Mock).mockReturnValue(rejectedPromise);

    // product create 함수 호출
    await createProduct(req, res, next);

    // 테스트: productController.createProduct() 함수에서 에러가 발생해서  next() 함수를 호출
    // next 함수를 호출할 때 errorMessage가 넘어가는지 확인하는 테스트
    expect(next).toBeCalledWith(errorMessage);
  });
  • Express에서 동기 요청에 관해 에러처리는 알아서 처리 / 하지만 비동기 처리에 대한 것은 서버가 망가질 수 있다.
  • 그래서 next() 함수를 통해 비동기 에러를 다른 에러 핸들러로 넘겨줍니다.

참고

  • 따라하며 배우는 TDD 개발 ( John Ahn )
profile
아직 나는 취해있을 수 없다...

0개의 댓글