Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<link href="http://code.jquery.com/qunit/qunit-git.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://builds.emberjs.com/handlebars-1.0.0.js"></script>
<script src="http://builds.emberjs.com/ember-1.0.0-rc.7.js"></script>
<meta charset=utf-8 />
<title>Ember Route Testing</title>
</head>
<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
</body>
</html>
 
//=====================================
// Source :
//=====================================
App = Em.Application.create({
});
App.UserEditRoute = Ember.Route.extend({
    model: function () {
        // here we tell the route to use its parent model 
        return this.modelFor('user');
    },
    activate: function () {
        this.controllerFor('user').set('editMode', true);
    },
    deactivate: function () {
        this.controllerFor('user').set('editMode', false);
    },
    events: {
        goBack: function () {
            this.transitionTo('user');
        }
    }
});
// defer readiness and set location's router to none in order to stop main application initialization
App.setupForTesting();
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//=====================================
// Test :
//=====================================
//-----------------
// First solution
//-----------------
(function () {
    var originalTemplates;
    var container;
    var Router;
    var router;
    function bootTestApplication() {
        router = container.lookup('router:main');
        Ember.run(TestApp, 'advanceReadiness');
    }
    function handleURL(path) {
        return Ember.run(function () {
            return router.handleURL(path).then(function (value) {
                ok(true, 'url: `' + path + '` was handled');
                return value;
            }, function (reason) {
                ok(false, 'failed to visit:`' + path + '` reason: `' + reason);
                throw reason;
            });
        });
    }
    module('First solution for test routes', {
        setup: function () {
            Em.run(function () {
                // We create a fake test Application to isalate route
                TestApp = Em.Application.create({
                    LOG_TRANSITIONS: true,
                    name: "TestApp",
                    rootElement: "#qunit-fixture"
                });
                // We defer initialization and disabled location
                TestApp.setupForTesting();
                // Save the router
                Router = TestApp.Router;
                // Save a loading route just in case
                TestApp.LoadingRoute = Ember.Route.extend({
                });
                // Save the global TestContainer to lookup  what you want
                container = TestApp.__container__;
                // We save the current ember templates to not impact the other unit test
                // when we will overide a few.
                originalTemplates = Ember.$.extend({}, Ember.TEMPLATES);
                // Override/mock application test for isolation
                Ember.TEMPLATES.application = Ember.Handlebars.compile("{{outlet}}");
                // You can here overide other templates if necessary
            });
        },
        teardown: function () {
            Ember.run(function () {
                // deleting the Test application
                TestApp.destroy();
                TestApp = null;
                // reset old Ember templates
                Ember.TEMPLATES = originalTemplates;
            });
        }
    });
    test('UserEditRoute', function () {
        Router.map(function () {
            this.resource('user', function () {
                this.route('edit');
            });
        });
        // Save the App.UserEditRoute to test into the isolated context
        TestApp.UserEditRoute = App.UserEditRoute;
        // we define a fake UserRoute who will return the expected model of the UserEditRoute
        var expectedUserModel = Em.Object.create({name: 'Model from user route'});
        TestApp.UserRoute = Em.Route.extend({
            model: function () {
                return expectedUserModel;
            }
        });
        // Mock of UserController and UserEditController classes
        TestApp.UserController = Em.Controller.extend({
            editMode: false
        });
      
        TestApp.UserEditController = Em.Controller;
        // We define an empty template
        Ember.TEMPLATES['user/edit'] = Ember.Handlebars.compile("");
        // Start TestApp application
        bootTestApplication();
        // retrieve UserController instance
        var userCtrl = container.lookup('controller:user');
        var userEditCtrl = container.lookup('controller:userEdit');
        handleURL('/user/edit');
        // assert activate hook behavior and model
        ok(userCtrl.get('editMode'));
        ok(userEditCtrl.get('content', expectedUserModel));
        // reset tested properties
        userCtrl.setProperties({
            editMode: true,
            content: null
        });
        handleURL('/');
        // assert deactivate hook behavior
        ok(!userCtrl.get('editMode'));
        // here we place other test like event handling, or to test others route's hooks
    });
})();
//-----------------
// Second solution
//-----------------
(function(){
  module('Second solution to test routes',{
    setup:function(){
    },
    teardown:function(){
    }
  });
  
  test('UserEditRoute',function(){
    var container = new Em.Container();
    var expectedUserModel = Em.Object.create({name: 'Model from user route'});
    
    container.register("controller:user", Em.Controller.extend({
      editMode: false
    }));
    container.register("route:user", Em.Route.extend({
      currentModel:expectedUserModel
    }));
    
    var userEditRoute = App.UserEditRoute.create({
      router : {
        container:container,
        router:{}
      },
      container:container
    });
    
    var userCtrl = container.lookup('controller:user');
    
    equal (userEditRoute.model(), expectedUserModel);
    userEditRoute.activate();
    equal (userCtrl.get('editMode'), true);
    userEditRoute.deactivate();
    equal (userCtrl.get('editMode'), false);
  });
})();
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
mbretonpro
0viewers