【发布时间】:2015-06-03 21:01:50
【问题描述】:
我要测试的代码是:
exports.hasTokenOrApi = (req, res, next) ->
if not req.headers?.authorization
return res.status(403).end()
new Promise (resolve, reject) ->
if req.headers.authorization.length is 32
# We are using an API key
global.db.User.find
where:
api_key: req.headers.authorization
.then (dbUser) ->
resolve dbUser.apiDisplay()
else
# We are using a redis session
req.redisSession.getAsync
app: 'sessions'
token: req.headers.authorization
.then (response) ->
resolve response.d
.then (user) ->
if not user.id
return res.status(403).end()
req.user = user
next()
.catch (err) ->
next err
这是一个中间件(我使用 Express),用于捕获各种 API 端点的令牌或 API 密钥。
到目前为止,我的测试是:
describe 'Authentication Middleware', ->
mock_res = {}
before (done) ->
mock_res =
status: ->
@
end: ->
@
global.db =
User:
find: ->
@
then: ->
id: 1
done()
it 'should return a 403 is no authorization is set in the header', ->
mock_req = {}
mock_next = null
status_spy = sinon.spy mock_res, 'status'
end_spy = sinon.spy mock_res, 'end'
authentication.hasTokenOrApi mock_req, mock_res, mock_next
status_spy.calledWith(403).should.equal true
end_spy.called.should.equal true
it.only 'should detect a valid API key', ->
mock_req =
headers:
authorization: 'e16b2ab8d12314bf4efbd6203906ea6c'
mock_next = sinon.spy()
authentication.hasTokenOrApi mock_req, mock_res, mock_next
mock_next.called.should.equal true
第一次测试很好,效果很好,解决了我所有的问题。
第二个工作不正常。我认为这与承诺有关?我的测试我们返回false,而我想做的是true
任何帮助将不胜感激!
【问题讨论】:
标签: unit-testing coffeescript promise mocha.js sinon