| id | name | birthday |
|----|-------|----------|
| 0 | Larry | 19960806 |
| 1 | Kyrie | 19960326 |
| 2 | John | 20001010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/api/item', (req, res) => { | |
console.log(req.body) | |
res.send(`${req.body.id} in body`) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const stubBar = sinon.stub(foo, 'bar').returns(50) | |
console.log(stubBar.called, stubBar.callCount) // false 0 | |
foo.bar() | |
console.log(stubBar.called, stubBar.callCount) // true 1 | |
foo.bar() | |
foo.bar() | |
console.log(stubBar.called, stubBar.callCount) // true 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const db = require('./db') | |
async function signup({ username, password }) { | |
// 1. 先檢查密碼長度是否大於 6 | |
if (password.length < 6) { | |
throw new Error('password is too short') | |
} | |
// 2. 再檢查 username 有沒有被用過 | |
if (await db.isUserExist(username)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const signup = require('./signup') | |
const db = require('./db') | |
// Test Case 1 | |
test('若密碼太短則註冊失敗,而且 db.isUserExist 不該被呼叫', async () => { | |
// 用 stub 把 db.isUserExist 替換掉 | |
const stubIsUserExist = sinon.stub(db, 'isUserExist') | |
// 給一個超短的 password | |
let user = { username: 'larry', password: 'pwd' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test Case 2 | |
test('若有重複的 username 則 註冊失敗,而且 db.createUser 不能被呼叫', async () => { | |
// 把 db.isUserExist 跟 db.createUser 都替換掉 | |
const stubIsUserExist = sinon.stub(db, 'isUserExist').returns(true) | |
const stubCreateUser = sinon.stub(db, 'createUser') | |
let user = { username: 'larry', password: 'pa55w0rd' } | |
// 確認註冊失敗 | |
await expect(signup(user)).rejects.toThrowError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test Case 3 | |
test('若帳號密碼都沒問題則註冊成功,而且 db.createUser 一定要被呼叫', async () => { | |
// 把 isUserExist 跟 createUser 都替換掉,並要求 isUserExist 回傳 false | |
const stubIsUserExist = sinon.stub(db, 'isUserExist').returns(false) | |
const stubCreateUser = sinon.stub(db, 'createUser') | |
let user = { username: 'larry', password: 'pa55w0rd' } | |
// 確認註冊成功 | |
await expect(signup(user)).resolves.toBe() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const signup = require('./signup') | |
const db = require('./db') | |
// Test Case 1: 若密碼長度太短,就不要再進資料庫找 User | |
test('should not call isUserExist if password is too short', async () => { | |
// 把 db.isUserExist 用 stub 替換掉 | |
const stubIsUserExist = sinon.stub(db, 'isUserExist') | |
// 給一個超短的 password | |
let user = { username: 'larry', password: 'pwd' } |
OlderNewer