我第一次尝试使用 buster.js 的 sinon.js,并且我正在尝试使用间谍来测试回调。
我的测试失败了,我猜 assert.calledOnceWith 正在使用 '===' 来比较预期与实际。
(coffeescript 中的所有内容)这是我的测试用例:
buster = require 'buster'
_ = require 'underscore'
routeParrot = require '../server/components/routeParrot'
buster.testCase 'routeParrot module',
setUp: (done) ->
this.socketioRequest =
method: 'get'
url: '/api/users'
headers: []
this.httpRequest =
method: 'get'
url: '/api/users'
headers: []
done()
# tearDown: (done) ->
# done()
'modifies http request to API': () ->
spy = this.spy()
routeParrot.http this.httpRequest, {}, (()->), spy
buster.assert.calledOnceWith spy,
_.extend(this.httpRequest, requestType: 'http'),
{jsonAPIRespond: (()->)},
->
这是我的错误:
[assert.calledOnceWith] Expected function spy() {} to be called once with arguments { headers: [], method: "get", requestType: "http", url: "/api/users" }, { jsonAPIRespond: function () {} }, function () {}
spy({ headers: [], method: "get", requestType: "http", url: "/api/users" }, { jsonAPIRespond: function () {} }, function () {})
作为参考,这是我的 routeParrot 模块:
module.exports.http = (req, res, next, router) ->
req.requestType = 'http'
if req.url.indexOf '/api' is 0
#api auth
#TODO
res.jsonAPIRespond = (json) ->
res.json json
router(req, res, next)
else
router(req, res, next)
module.exports.socketio = (req, res, router) ->
req.requestType = 'socketio'
httpEmulatedRequest =
method: if req.data.method then req.data.method else 'get'
url: GLOBAL.apiSubDir + (if req.data.url then req.data.url else '/')
headers: []
response =
jsonAPIRespond: (json) ->
req.io.respond json
#TODO api auth
router req, res, ->
如您所见,我正在尝试将对象文字与嵌入式函数进行比较。我是不是在这里偏离了基础,还是我必须做一些事情,比如覆盖在 calledOnceWith 中完成的比较?谢谢!