Looks like we can't reproduce the issue or there's a problem in the test case provided.
  1. #11
    Sencha User
    Join Date
    Apr 2011
    Posts
    65
    Vote Rating
    1
    c089 is on a distinguished road

      0  

    Default


    Did you get any further with testing controllers? I managed to create a instance of a controller by stubbing getModel, getStore and getView, but this only allows me to test methods that are really isolated from everything that's to do with extjs. For example, I cannot fire events and no event handlers are called ever

  2. #12
    Sencha User ykey's Avatar
    Join Date
    Mar 2010
    Location
    USA
    Posts
    245
    Vote Rating
    27
    ykey has a spectacular aura about ykey has a spectacular aura about

      0  

    Default


    Still working on it. Since 4.0.2 was just released with no unit testing examples I wouldn't count on seeing any even though Ed said we would.

  3. #13
    Sencha User ykey's Avatar
    Join Date
    Mar 2010
    Location
    USA
    Posts
    245
    Vote Rating
    27
    ykey has a spectacular aura about ykey has a spectacular aura about

      1  

    Default


    I am able to use Jasmine spies to spy on Controller control statements to be able to regression test their selectors. I am also able to test Controller refs.

    The life cycle of the control statements required me to create the spies before the Controller control method (init) is executed. I am still looking for a work around but do not think I will find one.

    More to come..

    Controller
    Code:
    Ext.define('TDD.controller.Main', {
        extend: 'Ext.app.Controller',
    
        refs: [
    	{
    	    ref: 'panel',
    	    selector: '#main'
    	}
        ],
    	
        init: function() {
            this.control({
    		'panel button[action="save"]': {
    			click: this.save
    		}
    	});				
        },
    
        save: function() {
    	console.log('Save clicked');
        }
    });
    Jasmine Spec
    Code:
    Ext.require([	
    	'TDD.controller.Main',
    	'TDD.view.Viewport'
    ]);	
    	
    describe("Main Controller", function() {
    	var application, controller, view;
      
    	beforeEach(function() {
    		application = Ext.create('Ext.app.Application', {
    			name: 'TDD'			
    		});
    		
    		controller = Ext.create('TDD.controller.Main', {
    			application: application
    		});
    		
    		spyOn(controller, 'save');
    
    		controller.init();						
    
    		view = Ext.create('TDD.view.Viewport', {
    			renderTo: Ext.getBody(),
    			listeners: {
    				render: function() {
    					controller.onLaunch();
    				}
    			}
    		});
    	});
    	
    	afterEach(function() {
    		view.destroy();
                    application = null;
    		controller = null;
    	});
    	
    	it("should be defined", function() {
    		expect(controller).not.toBeNull();
    	});	
    	
    	it("should have a ref to the panel", function() {
    		var panel = controller.getPanel();
    		expect(panel).not.toBeNull();
    	});
    		
    	it("should call save when save button is clicked", function() {
    		var button = controller.getPanel().down('button[action="save"]');				
    		button.fireEvent('click');		
    		
    		expect(controller.save).toHaveBeenCalled();
    	});
    });

  4. #14
    Sencha User ykey's Avatar
    Join Date
    Mar 2010
    Location
    USA
    Posts
    245
    Vote Rating
    27
    ykey has a spectacular aura about ykey has a spectacular aura about

      0  

    Default


    Also make sure you do this in your Jasmine SpecRunner.

    Code:
    Ext.onReady(function() {
    	jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
    	jasmine.getEnv().execute();
    });

  5. #15
    Sencha User ykey's Avatar
    Join Date
    Mar 2010
    Location
    USA
    Posts
    245
    Vote Rating
    27
    ykey has a spectacular aura about ykey has a spectacular aura about

      0  

    Default


    The other thing that I want to make sure I do is have my CI build running a SpecRunner that is executing the production Sencha SDK Tools build and not my Ext.Loader development build. That way I should be able to verify the build was created correctly.

  6. #16
    Sencha User
    Join Date
    Apr 2011
    Posts
    65
    Vote Rating
    1
    c089 is on a distinguished road

      0  

    Default


    Thank you so much! It works now. The two important hints were:
    - neccessity to call init() manually (I only created the controllerobject)
    - and having to set up the test dummies *before* doing this.

    Thanks again! You made my day

  7. #17
    Sencha - Community Support Team edspencer's Avatar
    Join Date
    Jan 2009
    Location
    Palo Alto, California
    Posts
    1,939
    Vote Rating
    9
    edspencer is a jewel in the rough edspencer is a jewel in the rough edspencer is a jewel in the rough

      0  

    Default


    There's a fair bit of back and forth here but it looks like this is just hard because Application and Controller are too tightly coupled. Aside from calling .init() manually did you have to do anything else to get this working how you want it?
    Ext JS Senior Software Architect
    Personal Blog: http://edspencer.net
    Twitter: http://twitter.com/edspencer
    Github: http://github.com/edspencer

  8. #18
    Sencha Premium Member
    Join Date
    Jul 2012
    Posts
    31
    Vote Rating
    1
    Qube is on a distinguished road

      1  

    Default Ext Spec

    Ext Spec


    I'm currently experimenting with a different approach to testing Ext classes that I hope will help with scenarios like this.

    Normally, gluing all the framework and application requirements together means you end up with a context much closer to integration testing. That's still very valuable of course, but not the same thing as unit testing.

    My goal is to achieve true unit isolation. I also want to make sure the solution is generic enough to work with QUnit, Jasmine, Sinon or whatever people use for BDD / TDD etc.

    Anyway, I'm calling the project Ext Spec, and so far it's going well. The source is up on CodePlex for anyone who's interested:
    https://extspec.codeplex.com/

  9. #19
    Sencha User
    Join Date
    Aug 2011
    Posts
    9
    Vote Rating
    0
    code_expert is on a distinguished road

      0  

    Default


    Hi,

    I still can't get the spy to work, even though I have a spy before controller init :-( Am I missing something obvious? I am a Jasmine NB and I appreciate some help.
    Code:
    it("should call a function when a button is clicked", function() {
            
            spyOn(ctlr, 'save');
            ctlr.init();
            
            var a = ctlr.getView('dashboard.HomePanel');
            var b = Ext.create(a);
            var button = b.down('#ds-factors-menu-id');
            button.fireEvent('Click');        
            
            expect(button.fireEvent('Click')).toBeTruthy();
            expect(ctlr.save).toHaveBeenCalled();
    
        });
    The click event seems to fire, but the save function is not being called.

Similar Threads

  1. ExtJS Jasmine unit test reporter?
    By Scott Murawski in forum Community Discussion
    Replies: 4
    Last Post: 9 Dec 2011, 12:53 PM
  2. Unit test cases
    By nivis1990 in forum Community Discussion
    Replies: 0
    Last Post: 9 Mar 2011, 9:05 PM
  3. Unit Test
    By sg707 in forum Ext: Discussion
    Replies: 1
    Last Post: 16 Feb 2011, 1:57 PM
  4. Howto unit test widgets?
    By KaiWeing in forum Ext GWT: Discussion
    Replies: 2
    Last Post: 9 Aug 2010, 8:33 AM
  5. JS Unit Test Panel
    By Ronaldo in forum Ext 2.x: User Extensions and Plugins
    Replies: 9
    Last Post: 20 Feb 2009, 9:32 AM

Thread Participants: 5