DanielS
DanielS

Reputation: 774

Unit testing ext js with Jasmine 2.0 and PhantomJS

I'm trying to setup unit testing for my ext js application.
I'm using Jasmine 2.0 and PhantomJS to run the tests from console.
I can successfully init the store in the init method of the controller.
But, if I try to declare it in the stores config, I'm getting the following error :
TypeError: 'null' is not a constructor (evaluating 'new c()') (line 1) (1) ,
What is the cause for the error, and how can it be resolved?

Thank you in advance.

My code is below:

TestApplication.js

Ext.Loader.setConfig({ enabled: true });
Ext.ns('myApp');

// Loading different components like controller, model, view..
Ext.application({
    name: 'myApp',
    appFolder: '../App',
    controllers: [],
    autoCreateViewport: false,

    init : function() {
        myApp.app = this;
    },

    // Launch Jasmine test environment
    launch: function () {
        var jasmineEnv = jasmine.getEnv();
        jasmineEnv.updateInterval = 1000;
        var htmlReporter = new jasmine.HtmlReporter();
        jasmineEnv.addReporter(htmlReporter);
        jasmineEnv.execute();
    }

});

spec.js

describe("myController", function () {
    var ctrl= null,
        store = null;

    beforeEach(function () {
        bmTab = Ext.create("myApp.controller.myController");
        bmTab.init();
    });
});

myController.js

Ext.define('myApp.controller.myController', {
    extend: 'Ext.app.Controller',

    //stores: [Stores.myStore];

    init:function() {
        console.log('**** init');
        var store = Ext.create(Stores.myStore);

        console.log('**** store created' + store);
    }    
});

Upvotes: 1

Views: 2303

Answers (1)

DanielS
DanielS

Reputation: 774

The problem was using Jasmine 2.0, when all of the tutorials were using Jasmine 1.3.
In Jasmine 2.0 a file boot.js was introduced.
And it was calling jasmine.getEnv().execute() on window.onload.
Because of that, specs were executing before Ext.launch was called. Once I removed the call to execute() from the boot.js it all started working. Below is a final version of my TestApplication.js code

P.S. Note that, HtmlReporter is also initialized in the boot.js, so there is no need to init it on the Ext.launch function

Ext.Loader.setConfig({ enabled: true });

Ext.application({
    name: 'myApp',
    appFolder: '../App',
    controllers: [],
    autoCreateViewport: false,

    // Launch Jasmine test environment
    launch: function () {
        var jasmineEnv = jasmine.getEnv();
        jasmineEnv.updateInterval = 1000;
        jasmineEnv.execute();
    }
});

Upvotes: 4

Related Questions