유저 회원 가입 test 작성
test 유저 데이터를 작성한다.
// __test__/fixtures/users.json
[
{
"name": "test_아이언맨",
"nickname": "test_빌런",
"email": "test_123@gmail.com",
"password": "test_password",
"gender": "test_남자",
"region": "test_서울 관악구 봉천동"
}
]
test가 실행되면 테이블을 비우고 만들어 놓은 test 유저 데이터를 생성한다.
beforeEach(async () => {
await Users.destroy({ where: {}, truncate: false });
await Users.create(usersFixture[0]);
});
회원 가입이 가능한 데이터를 해당 경로로 날려주는 함수를 작성한다.
describe('POST /user/signup', () => {
test('should respond user info to signup data', async done => {
const response = await request(app)
.post('/users/signup')
.send({
name: 'test',
nickname: 'test',
email: 'test@gmail.com',
password: 'test',
gender: 'test',
region: 'test',
});
expect(response.status).toEqual(200);
done();
});
먼저 넣어둔 유저 데이터와 비교하여 중복된 회원을 검사하는 함수를 작성한다.
test('should respond conflict with existing user email', async done => {
const response = await request(app)
.post('/users/signup')
.send({
name: 'test_아이언맨',
nickname: 'test_빌런',
email: 'test_123@gmail.com',
password: 'test_password',
gender: 'test_남자',
region: 'test_서울 관악구 봉천동',
});
expect(response.status).toEqual(409);
done();
});
코드
import request from 'supertest';
import { Users } from '../models/Users';
const usersFixture = require('../__test__/fixtures/users.json');
describe('Bare Minimum Requirements', () => {
beforeEach(async () => {
await Users.destroy({ where: {}, truncate: false });
await Users.create(usersFixture[0]);
});
describe('POST /user/signup', () => {
test('should respond user info to signup data', async done => {
const response = await request(app)
.post('/users/signup')
.send({
name: 'test',
nickname: 'test',
email: 'test@gmail.com',
password: 'test',
gender: 'test',
region: 'test',
});
expect(response.status).toEqual(200);
done();
});
test('should respond conflict with existing user email', async done => {
const response = await request(app)
.post('/users/signup')
.send({
name: 'test_아이언맨',
nickname: 'test_빌런',
email: 'test_123@gmail.com',
password: 'test_password',
gender: 'test_남자',
region: 'test_서울 관악구 봉천동',
});
expect(response.status).toEqual(409);
done();
});
});
});
npm test
'Project > TIL' 카테고리의 다른 글
20.03.25 ShareBook TIL (0) | 2020.03.25 |
---|---|
20.03.24 ShareBook TIL (0) | 2020.03.25 |
20.03.22 ShareBook TIL (0) | 2020.03.22 |
20.03.21 ShareBook TIL (0) | 2020.03.22 |
20.03.20 ShareBook TIL (0) | 2020.03.20 |