I'm using Karma, Jasmine, Jasmine.Async, Sinon and Chai.
The good news...this test works correctly. The dependency is mocked, spies get called, and intentionally breaking the test subject results in failed tests.
define(['chai', 'squire'], function (chai, Squire) {
var should = chai.should(),
async = new AsyncSpec(this),
subject, injector = new Squire();
describe('EventsView', function () {
describe('when an event is clicked', function () {
var mockModel, stub;
async.beforeEach(function (done) {
setFixtures('<div id="screen"></div>');
mockModel = {
toJSON: function () {
return {
dimensions: "hu1 vu2",
events: [{
date: "8/29/2013",
id: "8923",
title: "Fancy Show",
venue: "Lovely venue",
}, {
date: "8/29/2013",
id: "9034",
title: "Exciting Game",
venue: "Lovely stadium"
}],
id: 3566,
kind: "events",
title: "Top events this week"
};
},
fetch: function () {}
};
stub = sinon.stub();
injector.mock('tiles/events-tile/events-detail-model', Squire.Helpers.constructs({
fetch: stub
}));
injector.require(["tiles/events-tile/events-view"], function (ev) {
subject = new ev(mockModel);
done();
});
});
async.afterEach(function (done) {
injector.clean();
injector.remove();
done();
});
async.it('should attempt to fetch the event details', function (done) {
$('#screen').html(subject.$el);
$('.event').first().click();
stub.called.should.be.true;
done();
});
});
});
});
The bad news...a shed load of other tests that were previously fine are now failing for weird reasons. For example:
Error: Backbone.history has already been started
and
TypeError: 'undefined' is not an object (evaluating 'Backbone.Validation.mixin')
If I comment out the snippet
injector.require(["tiles/events-tile/events-view"], function (ev) {
subject = new ev(mockModel);
done();
});
Then the other tests work again. I've had stuff like this happen before and it has usually been down to a sinon mock not getting restored. The injector.clean()
call doesn't seem to be providing the magic bullet I was hoping for.