我正在使用 Karma、Jasmine、Jasmine.Async、Sinon 和 Chai。
好消息……这个测试工作正常。依赖项被嘲笑,间谍被调用,故意破坏测试对象会导致测试失败。
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();
});
});
});
});
坏消息......以前很好的其他测试现在因奇怪的原因而失败。例如:
Error: Backbone.history has already been started
和
TypeError: 'undefined' is not an object (evaluating 'Backbone.Validation.mixin')
如果我注释掉片段
injector.require(["tiles/events-tile/events-view"], function (ev) {
subject = new ev(mockModel);
done();
});
然后其他测试再次工作。我以前也发生过这样的事情,通常是因为 sinon mock 没有得到恢复。这个injector.clean()
电话似乎没有提供我所希望的灵丹妙药。