1. #1
    Sencha Premium Member twasink's Avatar
    Join Date
    Oct 2012
    Location
    Brisbane, Australia
    Posts
    78
    Vote Rating
    8
    twasink will become famous soon enough

      0  

    Default [4.2.0-489] Unit testing controllers - specifically event handlers

    [4.2.0-489] Unit testing controllers - specifically event handlers


    One of the promoted advantages of 4.2.0, as per the announcement post, is that controllers can be unit tested.

    I have certainly found that I can easily instantiate a controller and unit test the methods without problems. However, I'm at a loss as to how to unit test the event listeners.

    Given a controller and Jasmine unit test like this:
    Code:
    Ext.define("TestApp.controller.TestController", {
        extend : 'Ext.app.Controller',
    
    
        init : function() {
            this.control({
                'button' : { click : this.onClick }
            });
        },
    
    
        onClick : function(btn, e, eOpts) {
            console.log("Button was clicked");
        }
    })
    
    
    describe("Test Controller", function() {
        it("should be able to be created", function() {
            var test = Ext.create('TestApp.controller.TestController');
            
            spyOn(test, 'onClick');
            
            test.init();
            
            // Do something _here_ to simulate a button clikc
            
            expect(test.onClick).toHaveBeenCalled();
        });
    });
    what would be the best thing to do to trigger the event handler? Short of instantiating a view class that matches the selector (which would normally be more complex, of course)

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,522
    Vote Rating
    814
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    The EventBus is not put onto the controllers themselves instead of getting them from the application. Before the controller would have to go to the application to set the listeners but now the bus is put on each controller.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha - Ext JS Dev Team
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    176
    Vote Rating
    34
    nohuhu has a spectacular aura about nohuhu has a spectacular aura about

      0  

    Default


    Controller testing isn't easy because of the number of components involved. If you consider how Controllers work, there are two major things to test: component selectors/ref selectors and controller methods. The latter can be tested in the usual way, whereas component selectors depend on Ext.ComponentQuery, which in turn depends on actual components being instantiated for queries to be meaningful.

    Starting from this point, I'd suggest breaking Controller tests into two suites: first would instantiate the relevant View(s) and run over component selectors to test that they actually return Views in question; the second would test everything else. If your views are too heavy to be instantiated for testing, you can use mockups that replicate the structure of your view without adhering to the same layout constraints.

    If and when you find yourself with a View that's too big to be tested this way, that would mean it's a good candidate for refactoring.

    Just my 2ยข anyway.

    Regards,
    Alex.

Thread Participants: 2