【问题标题】:Why is callback atttached to an event called twice?为什么回调附加到一个事件被调用两次?
【发布时间】:2014-02-13 09:23:58
【问题描述】:

我正在梳理this 教程,我很好奇为什么回调被多次调用——在这种情况下是两次。这是我的代码:

'use strict';

var chai = require('chai'),
    expect = chai.expect,
    sinon = require('sinon'),
    sinonChai = require('sinon-chai');

var Backbone = require('backbone'),
    _ = require('lodash/dist/lodash.underscore');

chai.use(sinonChai);

describe('Backbone.Events', function() {
  var myObj;

  beforeEach(function() {
    myObj = {};
    _.extend(myObj, Backbone.Events);
  });

  it('allows us to bind and trigger custom named events on an object', function() {
    var callback = sinon.spy();

    myObj.bind('custom event', callback);
    myObj.trigger('custom event');

    expect(callback).to.have.been.called;      // => passes
    expect(callback).to.have.been.calledOnce;  // => fails??
    expect(callback).to.have.been.calledTwice; // => passes, why?
  });
});

有什么见解吗?

【问题讨论】:

    标签: node.js backbone.js mocha.js sinon chai


    【解决方案1】:

    你正在绑定并触发两个事件custom eventcustomevent

    尝试将其更改为custom_event

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-30
      • 2012-08-02
      • 2011-06-05
      • 1970-01-01
      • 2017-01-22
      • 2021-07-13
      • 2021-11-25
      • 2015-12-24
      相关资源
      最近更新 更多