Testing routes

Testing routes can be done either by acceptance tests or unit tests. In this example, we'll create a simple unit test for a route.

How to do it...

  1. In a new application, generate a new students route:
    $ ember g route students
    

    This command will generate the route code for students.

  2. Edit the students route information and add a new property:
    // app/routes/students.js
    import Ember from 'ember';
    
    export default Ember.Route.extend({
        someText: 'someText'
    });

    This route has a property called someText.

  3. Edit the students-tests.js file in the tests/unit/routes folder:
    // tests/unit/routes/students-test.js
    import { moduleFor, test } from 'ember-qunit';
    
    moduleFor('route:students', 'Unit | Route | students', {
        // Specify the other units that are required for this test.
    });
    
    test('check prop and route exists', function(assert) {
        let route = this.subject();
        assert.expect(3);
        assert.equal(route.get('someText'),'someText');
        route.set('someText','otherText');
        assert.equal(route.get('someText'),'otherText');
        assert.ok(route);
    });

    In this example, we are checking the output of the someText property. The first assert.equal gets the property and checks it against the someText value. The route instance can also set properties. The next assert checks to see whether the new value has been set. The final assert makes sure that the route is available.

  4. Run ember server and navigate to /tests:
    How to do it...

    This shows all the tests passed

How it works...

The route in your Ember application has a few different functions. It can hold your model data and have properties and actions. When testing routes, we can use it with a more general acceptance test or as an individual unit test.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset