首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在单元测试中使用间谍(sinon.js和buster.js)

在单元测试中使用间谍(sinon.js和buster.js)
EN

Stack Overflow用户
提问于 2013-04-03 02:41:12
回答 1查看 493关注 0票数 0

我第一次尝试在buster.js中使用sinon.js,并且我尝试使用间谍来测试回调。

我的测试失败了,我猜assert.calledOnceWith正在使用'===‘来比较expected和actual。

(coffeescript中的所有内容)下面是我的测试用例:

代码语言:javascript
复制
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: (()->)},
      ->

下面是我的错误:

代码语言:javascript
复制
[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模块作为参考:

代码语言:javascript
复制
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中完成的比较?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-03 15:16:23

问题是你试图比较函数不同的函数。因此,您在断言中创建的空函数不能与调用您的spy时使用的函数相同。

此外,为了提高测试失败的可读性,您应该拆分断言,以便使用单个断言测试每个参数。否则,很难辨别出哪个参数是错误的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15771832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档