-
TIL 194일 - sequelize mock공부/JavaScript 2022. 7. 11. 22:03
새로운 Sequelize 프로젝트
일단은 다른 스택들은 제외하고 테스트를 해보는 게 좋을 거 같아서, 새로운 sequelize 프로젝트를 만들었다.
import express from "express"; import { DataTypes, Sequelize } from "sequelize"; const app = express(); const port = 3000; const sequelize = new Sequelize("node_test", "user_name", "password", { host: "localhost", dialect: "postgres", }); const User = sequelize.define( "User", { // Model attributes are defined here firstName: { type: DataTypes.STRING, allowNull: false, }, lastName: { type: DataTypes.STRING, // allowNull defaults to true }, }, { tableName:"User" // Other model options go here } ); app.get("/", async (req: any, res: any) => { try { await User.create({ firstName: "firstName", lastName: "lastName", }); } catch (e) { console.log(e); } res.send("Hello World!"); }); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
Sequelize-mock
https://sequelize-mock.readthedocs.io/en/stable/
해보니까 mocha랑 sequelize mock을 사용해서 이렇게 코드를 만들 수 있긴 하다.
음... 근데 내가 상상하던 것은 아닌데...?
let SequelizeMock = require("sequelize-mock"); let DBConnectionMock = new SequelizeMock(); const UserMock = DBConnectionMock.define( "users", { email: "email@example.com", username: "blink", picture: "user-picture.jpg", }, { instanceMethods: { myTestFunc: function () { return "Test User"; }, }, } ); describe("test", () => { it("test", async () => { const result = await UserMock.findOne({ where: { username: 'my-user', }, }) console.log(result); }); });
sinon.js를 알아보는 게 나을 거 같다.
'공부 > JavaScript' 카테고리의 다른 글
TIL 196일 - node에서 presentation 레이어 유닛 테스트 구현해보기 (0) 2022.07.13 TIL 195일 - sinon.js, chai, mocah (0) 2022.07.12 TIL 190일 - node.js 테스트 코드... (0) 2022.07.07 TIL 189일 - node.js unit test 툴 (0) 2022.07.07 TIL 136일 - next.js header (0) 2022.05.14