我第一次尝试在buster.js中使用sinon.js,并且我尝试使用间谍来测试回调。
我的测试失败了,我猜assert.calledOnceWith正在使用'===‘来比较expected和actual。
(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中完成的比较?谢谢!
发布于 2013-04-03 15:16:23
问题是你试图比较函数不同的函数。因此,您在断言中创建的空函数不能与调用您的spy时使用的函数相同。
此外,为了提高测试失败的可读性,您应该拆分断言,以便使用单个断言测试每个参数。否则,很难辨别出哪个参数是错误的。
https://stackoverflow.com/questions/15771832
复制相似问题