Question

I am attempting to run some Jasmine unit tests for a Backbone view, mocking out dependencies in Squire.

The dependencies of my view are a Baseview, an ICanHaz template and an i18n translation.

I mock out the dependencies after defining Squire and Backbone, and then use a Squire injector to require my view. However, when I run the tests through Grunt, I get the warning message:

Warning: No specs executed, is there a configuration error? Use --force to continue.

Here is my spec:

define(['squire', 'backbone'], function (Squire, Backbone) {
    var injector = new Squire();

    mocks = {
        'views/baseview': function () {
            return Backbone.View.extend({
                grabTemplate: function (options) { }
            });
        },
        'text!templates/menu.htm': '',
        'i18n!nls/menu': {}
    };

    injector.mock(mocks);

    injector.require(['menu'], function (Menu) {

        describe('Menu View', function () {

            it('should be initialisable', function () {
                var menu = new Menu();
                expect(menu).toBeDefined();
            });
        });
    });
});

Does anyone know why my basic unit test is not getting picked up?

Was it helpful?

Solution

After a bit of trial and error, I found a solution I'm vaguely happy with. I modified the solution from this post, utilising Jasmine's run and waitsFor calls:

define(['squire', 'backbone'], function (Squire, Backbone) {

    describe('Menu View module', function () {

        it('should be initialisable', function () {

            var injector = new Squire(),
            mocks,
            menu,
            loaded = false; //track whether our module has loaded

            mocks = {
                'views/baseview': function () {
                  return Backbone.View.extend({
                      grabTemplate: function (options) { }
                  });
                },
                'text!templates/menu.htm': '',
                'i18n!nls/menu': {}
            };

            injector.mock(mocks);

            injector.require(['views/menu'], function (Menu) {
                menu = new Menu();
                loaded = true;
            });

            waitsFor(function() {
                return loaded;
                //when this is true, we'll drop out of waitsFor
            }, 'Menu module not loaded', 10000);

            runs(function() {
                expect(menu).toBeDefined();
            });
          });
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top