fix(users): Achieved 100% test coverage of users controller

This commit is contained in:
Chris Troutner
2020-11-12 07:56:13 -08:00
parent ba44070650
commit 392b1b89ca
2 changed files with 41 additions and 1 deletions
+7 -1
View File
@@ -170,12 +170,18 @@ class UserController {
user
}
} catch (err) {
if (err === 404 || err.name === 'CastError') {
// Handle different error types.
if (
err === 404 ||
err.name === 'CastError' ||
err.message.toString().includes('Not Found')
) {
ctx.throw(404)
}
ctx.throw(500)
}
if (next) {
return next()
}
+34
View File
@@ -374,6 +374,40 @@ describe('Users', () => {
'Password property should not be returned'
)
})
it('should catch and handle errors', async () => {
try {
// Force an error
sandbox.stub(uut.User, 'findById').rejects(new Error('test error'))
// Mock the context object.
const ctx = mockContext()
await uut.getUser(ctx)
assert.fail('Unexpected result')
} catch (err) {
assert.include(err.message, 'Internal Server Error')
}
})
it('should handle user not found', async () => {
try {
// Force an error
sandbox.stub(uut.User, 'findById').resolves(false)
// Mock the context object.
const ctx = mockContext()
ctx.params = { id: 1 }
await uut.getUser(ctx)
assert.fail('Unexpected result')
} catch (err) {
console.log(err)
assert.include(err.message, 'Not Found')
}
})
})
describe('PUT /users/:id', () => {