Get help with testing, discuss unit testing strategies etc.


Post by AaronTovo »

I get an 'undefined' error when I try to construct one of my controllers. It says that this.application is undefined. I don't explicitly create an application object because I only want to test a part of my code. Do I need to create some kind of test application test my controllers?

This is the line in ext-all-debug.js that has the error:
    
getStore: function(name) {
        return this.application.getStore(name);   // Uncaught TypeError: Cannot call method 'getStore' of undefined
}
Here is my Harness setup:
var Harness = Siesta.Harness.Browser.ExtJS;

Harness.configure({
    title       : 'Awesome Test Suite',
    loaderPath  : { 'StudySearch' : 'study-search' },
    waitForAppReady: false,
    expectedGlobals: [
        'Ext',
        'VI',
        'TR'
    ],
    preload     : [
        "siesta-test/lib/ext4/resources/vitaltheme/css/vitaltheme.css",
        "siesta-test/lib/ext4/ext-all-debug.js",
        "siesta-test/lib/ext4/locale/ext-lang-en.js",
        "study-search/Utils.js"
    ]
});

Harness.start(
    {
        hostPageUrl: 'siesta',
        url: 'siesta-test/010_sanity.t.js'
    },
    'siesta-test/020_basic.t.js',
    {
        hostPageUrl: 'siesta',
        url: 'siesta-test/030_archive.t.js'
    }
);
Here's the code for the test itself:
StartTest(function(t) {
    var store, view;

    t.chain(
        function (next) {
            t.requireOk('StudySearch.store.Archives', next);
        },
        function (next) {
            store = Ext.create('StudySearch.store.Archives', {});
            store.model.proxy.url = './siesta-test/mock/archive.js';
            t.loadStoresAndThen(store, next);
        },

        // test the store
        function(next) {

            // store/model is loaded and tested.  now test the archive list view
            t.requireOk('StudySearch.view.ArchivesList', next);
        },

        // test the view
        function(next) {
            view = Ext.create('StudySearch.view.ArchivesList', { store: store});
            t.ok(view, 'ArchivesList view created');

            t.requireOk('StudySearch.controller.Filter', next);
        },

        // test the controller
        function(next) {
            var ctlr = Ext.create('StudySearch.controller.Filter', {});    /////////// This is where the error happens
          //  t.ok(ctlr, 'Filter controller created');
        }
     ); // end chain
})
The controller definition starts out like:
Ext.define('StudySearch.controller.Filter', {
    extend: 'Ext.app.Controller',
    stores: ['Archives', 'SearchResults', 'DateRanges', 'Modalities'],
    views: ['ArchivesList', 'SearchForm'],
...
});

Post by mats »

Yes, you'll need either a 'real' application created or you can try to mock the Application instance since you have a hard dependency on it.

Post by AaronTovo »

Is there an example of testing with a mock application somewhere? Or even testing a controller? I can't find any. Is that something that's generally not done with extjs code? I have a lot of code in my controllers for handling various events so testing controllers seems like a sensible thing to do.

I tried the following, and several variations of it, but I can't seem to get a handle for a controller that is bound to the mock application.
        // test the controller
        function(next) {
            var app = Ext.application({
                name: 'JVES',
                appFolder: '/vitrea-home/j-ves',
                controllers: [ 'Filter' ]
            });
            var ctlr = app.getController('Filter');
        }

Post by AaronTovo »

I got it working with
            var ctlr;
            var app = Ext.application({
                name: 'JVES',
                appFolder: '/vitrea-home/j-ves',
                controllers: [ 'Filter' ],
                launch: function() {
                    var me = this;
                    ctlr = me.getController('Filter');
                }
            });
            t.ok(ctlr, 'now we can test the Filter controller with the mock app');

Post Reply