Skip to content

Instantly share code, notes, and snippets.

@LarryLuTW
Last active August 10, 2020 03:05
Show Gist options
  • Save LarryLuTW/191fb79b9e2e38d40e2d7b8a28a0dbe3 to your computer and use it in GitHub Desktop.
Save LarryLuTW/191fb79b9e2e38d40e2d7b8a28a0dbe3 to your computer and use it in GitHub Desktop.
// 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()
// 確認 db.isUserExist 有被呼叫,但 db.createUser 沒有被呼叫
expect(stubIsUserExist.called).toBe(true)
expect(stubCreateUser.called).toBe(false)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment