2013-04-02 34 views
0

我第一次尝试用buster.js与sinon.js,我试图用间谍来测试回调。使用间谍与单元测试(sinon.js&buster.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内完成的比较?谢谢!

+0

阅读关于“匹配器”的sinon.js文档...我想我的答案在这里... –

回答

1

问题是您尝试比较功能不同的功能。所以你在断言中创建的空函数不能与你的间谍被调用的空函数相同。

为了更好地读取测试失败,您应该分解断言,以便使用单个断言测试每个参数。否则很难找出哪个参数是错误的。

相关问题