gmajivu
gmajivu

Reputation: 1310

Why is callback atttached to an event called twice?

I am combing through this tutorial and I am curious why the callback is called multiple times - twice in this case. Here is my code:

'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?
  });
});

Any insights?

Upvotes: 0

Views: 294

Answers (1)

Rida BENHAMMANE
Rida BENHAMMANE

Reputation: 4129

You are binding and triggering two events custom event custom and event

try to change it to custom_event

Upvotes: 1

Related Questions