-
6 Mar 2013 3:09 AM #1
[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:
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)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(); }); });
-
8 Mar 2013 7:26 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,656
- Vote Rating
- 435
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
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
8 Mar 2013 4:17 PM #3
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.


Reply With Quote