0

初めて buster.js で sinon.js を試しています。スパイを使用してコールバックをテストしようとしています。

テストが失敗し、assert. calledOnceWith が「===」を使用して予想と実際を比較していると推測しています。

(コーヒースクリプトのすべて) ここに私のテストケースがあります:

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内で行われた比較をオーバーライドするようなことをしなければなりませんか? ありがとう!

4

1 に答える 1

1

問題は、関数の異なる関数を比較しようとすることです。したがって、アサーションで作成する空の関数は、スパイが呼び出された関数と同じにすることはできません。

また、テストの失敗を読みやすくするために、アサーションを分割して、すべてのパラメーターを単一のアサーションでテストする必要があります。そうしないと、どのパラメーターが間違っているかを見つけるのが難しくなります。

于 2013-04-03T07:16:22.510 に答える