3

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?

Simon Adcock
  • 3,554
  • 3
  • 25
  • 41

1 Answers1

1

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();
            });
          });
    });
});
dansomething
  • 693
  • 8
  • 7
Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
  • 1
    @dansomething Why was this edited for the sole purpose of turning it from 8 space indenting to 4? That is not an edit that is reflective of improving quality, but rather an edit reflecting a personal style preference. – scragar May 15 '14 at 16:05
  • 2
    The indentation change was unintended. The closing curly braces were missing and miss-aligned. – dansomething May 15 '14 at 16:11