Skip to content

Instantly share code, notes, and snippets.

View LarryLuTW's full-sized avatar
🏠
Working from home

Larry Lu LarryLuTW

🏠
Working from home
View GitHub Profile

Database 格式

| id | name  | birthday |
|----|-------|----------|
| 0  | Larry | 19960806 |
| 1  | Kyrie | 19960326 |
| 2  | John  | 20001010 |
@LarryLuTW
LarryLuTW / git.md
Last active March 30, 2017 11:25 — forked from w5151381guy/git.md

git 基本觀念

  1. git 為一種版本控制軟體,所謂版本控制就是你可以針對在檔案中,你曾經動過的地方做控管,也就是說你可以隨時回到之前的版本中
  2. 為了達到第一點的方便性,請在做完一個段落後進行 commit,commit 的內容一定要寫得相當清楚,否則你會不知道你曾經改過甚麼東西
  3. 在 commit 之前需先將改過的東西加到 staging area,然後再用 commit 進行提交
  4. 養成一個好習慣,在準備將檔案上傳(push)到 server 前,記得先檢查一下狀態,語法會在底下說明

git 基本語法

@LarryLuTW
LarryLuTW / example.js
Created November 7, 2017 05:28
syntax-highlight
app.post('/api/item', (req, res) => {
console.log(req.body)
res.send(`${req.body.id} in body`)
})

無關語言

搜尋/取代/Regex

  • 全域搜尋(在專案內所有檔案內搜尋)
  • 特定檔案搜尋(在所有 js 檔裡面搜尋)
  • 特定檔案外搜尋(在除了 js 檔之外的檔案內搜尋)
  • 單檔部分內容搜尋(在某個檔案中的 10 - 50 行做搜尋)

git

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
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)) {
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' }
// 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()
// 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()
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' }