node-mocks-http, beforeEach

2023. 2. 23. 17:27·Node.js/TDD
반응형

 

단위테스트에서 http 객체 (request, response)를 얻으려면 node-mocks-http를 이용하면 된다.

npm install node-mocks-http
let req = httpMocks.createRequest();
let res = httpMocks.createResponse();

 

이전 포스팅에(2023.02.23 - [Node.js/TDD] - 단위 테스트 create) 이어서 createProduct에 req.body를 넣어주기 위해서

node-mocks-http를 이용해 req와 res를 선언하고

req.body에 넣어줄 json 데이터 파일을 생성해서 req.body에 할당후,

productModel.create 메서드가 newProduct와 함께 호출되는지 체크해주었다.

 

const productController = require("../../controller/products");
const productModel = require("../../models/Product");
const httpMocks = require("node-mocks-http");
const newProduct = require("../data/new-product");

productModel.create = jest.fn();

describe("Product Controller Create", () => {
  it("should have a createProduct function", () => {
    expect(typeof productController.createProduct).toBe("function");
  });
  it("should call ProductModel.create", () => {
    let req = httpMocks.createRequest();
    let res = httpMocks.createResponse();
    let next = null;
    req.body = newProduct;

    productController.createProduct(req, res, next); // createProduct 가 호출 될 때
    expect(productModel.create).toBeCalledWith(newProduct); // 이 메소드가 실행되는지 체크
  });
});

 

 

beforeEach

여러 개의 테스트에 공통된 코드가 있다면 beforeEach 안에 넣어서 반복을 줄여줄 수 있다.

위 코드에 적용하면 다음과 같이 여러 테스트 코드에서 사용 가능하다.

const productController = require("../../controller/products");
const productModel = require("../../models/Product");
const httpMocks = require("node-mocks-http");
const newProduct = require("../data/new-product");

productModel.create = jest.fn();

let req, res, next;

beforeEach(() => {
  req = httpMocks.createRequest();
  res = httpMocks.createResponse();
  next = null;
});

describe("Product Controller Create", () => {
  beforeEach(() => {
    req.body = newProduct;
  });
  it("should have a createProduct function", () => {
    expect(typeof productController.createProduct).toBe("function");
  });
  it("should call ProductModel.create", () => {
    productController.createProduct(req, res, next); // createProduct 가 호출 될 때
    expect(productModel.create).toBeCalledWith(newProduct); // 이 메소드가 실행되는지 체크
  });
});

 

반응형

'Node.js > TDD' 카테고리의 다른 글

통합 테스트(Intergration Test)  (0) 2023.03.01
단위 테스트 create  (0) 2023.02.23
TDD ( Test Driven Development )  (0) 2023.02.21
'Node.js/TDD' 카테고리의 다른 글
  • 통합 테스트(Intergration Test)
  • 단위 테스트 create
  • TDD ( Test Driven Development )
jjikky
jjikky
  • jjikky
    jikky.env
    jjikky
  • 전체
    오늘
    어제
    • 분류 전체보기
      • React
      • Node.js
        • TDD
        • Node.js
        • mern
        • OAuth
        • js_facebook login
      • Coding Test
        • 백준 알고리즘
        • CodeUp
        • 코테 이론
      • Js
        • Javascript
      • study
        • python
        • android
        • Big data analysis
        • Logic Circuit
      • git
      • 개발일지
      • 게임기획
      • Docker
      • IPFS
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    파이썬 그리디
    nft
    Python
    ipfs add
    git 유용한 명령어
    verilog
    그리디 알고리즘
    파이썬
    NFT IPFS
    빅데이터
    범주형 자료
    NFT Marketplace
    ifps 네트워크 지연
    verilog할당문
    파이썬 딕셔너리
    안드로이드
    Ipfs
    코딩테스트
    UI
    파이썬 완전탐색
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
jjikky
node-mocks-http, beforeEach
상단으로

티스토리툴바