javascript - Squire is breaking other tests - Stack Overflow

I'm using Karma, Jasmine, Jasmine.Async, Sinon and Chai.The good news...this test works correctly.

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 ment 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.

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 ment 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.

Share Improve this question asked Sep 15, 2013 at 10:34 Dom BarkerDom Barker 1,9181 gold badge16 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

In my experience, Squire causes way more headaches than it solves. Are you using any jQuery plugins? I've found that they don't play well with squire and can cause subsequent tests to fail. If that's the case, check out this blog about how to deal with that.

My remendation, however, is to ditch Squire entirely. Using sinon and creating your own utilities to mock exactly what you need should not only make your asynchronous tests synchronous again (where the code under test is synchronous), but it will also encourage the creation of more testable code. It should also help you understand your code and libraries at a deeper level-- stubbing Backbone.history to perform routing actions without actually triggering routes is a great exercise and a fantastic utility to add to your test library.

The problem is with non AMD modules. If I remember correctly when Squire creates a new context it will create a new instance of the non AMD module in the global namespace. The result is that your code and test will reference a different object. I think I managed to solve this problem by doing:

var injector = new Squire();
injector.mock("backbone", function() {
  return Backbone;
});

Squire should handle non AMD modules better.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745283783a4620436.html

相关推荐

  • javascript - Squire is breaking other tests - Stack Overflow

    I'm using Karma, Jasmine, Jasmine.Async, Sinon and Chai.The good news...this test works correctly.

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信