POST /api/posts API
=> PostsController의createPost
Method
게시글 생성의 경우 nickname, password, title, content 4개의 데이터를 입력받고, postId, nickname, title, content, createdAt, updatedAt이 포함된 생성된 게시글의 정보를 반환받습니다.
아무런 데이터를 전달하지 않았던, GET Method와 다르게, 이번 POST Method에서는 body 데이터를 전달하고 있습니다.
test("POST /api/posts API (createPost) Integration Test Success Case", async () => {
const createPostBodyParams = {
nickname: "Nickname_Success",
password: "Password_Success",
title: "Title_Success",
content: "Content_Success",
};
const response = await supertest(app)
.post(`/api/posts`) // API의 HTTP Method & URL
.send(createPostBodyParams); // Request Body
// 1. API의 status가 201
expect(response.status).toEqual(201);
// 2. API의 res 데이터는 { postId, nickname, title, content, createdAt, updatedAt }
expect(response.body).toMatchObject({
data: {
postId: 1,
nickname: createPostBodyParams.nickname,
title: createPostBodyParams.title,
content: createPostBodyParams.content,
createdAt: expect.anything(), //null, undefined만 아니면 된다. => 존재하기만 하면 된다.
updatedAt: expect.anything(),
},
});
});
test("POST /api/posts API (createPost) Integration Test Error Case, Invalid Params Error", async () => {
const response = await supertest(app)
.post(`/api/posts`) // API의 HTTP Method & URL
.send(); // Request Body
// 1. API의 status가 400
expect(response.status).toEqual(400);
// 2. API의 res 데이터는 { errorMessage: error.message }
expect(response.body).toEqual({ errorMessage: "InvalidParamsError" });
});
response.body
안에 객체(Object)의 형태로 존재하고 있습니다.response.body
데이터 뿐만 아니라, Response Headers와 같이 다른 반환된 정보들을 검증할 때에도 사용됩니다.expect().toMatchObject({})
문법을 이용해 데이터를 검증할 수 있습니다.null
, undefined
과 일치하는지 검증할 때 사용됩니다.createdAt
, updatedAt
과 같이 객체의 특정 Parameter가 존재하는지에 대한 여부를 검증하기 위해 사용합니다.