Controllers should be tested in your application. In this recipe, we'll test some basic actions from a controller.
index
controller:$ ember g controller index
This creates a new controller called index
.
// app/controllers/index.js import Ember from 'ember'; export default Ember.Controller.extend({ myValue: 'value', actions:{ pressed(value){ this.set('myValue',value); } } });
This controller has one property named myValue
. Another action called pressed
changes the value of myValue
to whatever value is passed in the function.
// tests/unit/controllers/index-test.js import { moduleFor, test } from 'ember-qunit'; moduleFor('controller:index', 'Unit | Controller | index', { // Specify the other units that are required for this test. // needs: ['controller:foo'] }); // Replace this with your real tests. test('it exists', function(assert) { let controller = this.subject(); assert.ok(controller); }); test('check property', function(assert) { assert.expect(2); let controller = this.subject(); assert.equal(controller.get('myValue'),'value'); controller.send('pressed','newValue'); assert.equal(controller.get('myValue'),'newValue'); });
moduleFor
helper is used here for controllers. The first test checks to make sure that the controller exists:… test('check property', function(assert) { assert.expect(2); let controller = this.subject(); assert.equal(controller.get('myValue'),'value'); controller.send('pressed','newValue'); assert.equal(controller.get('myValue'),'newValue'); });
This test creates an instance of the controller with this.subject
. The initial value is checked to make sure that it's correct. To send an action to the controller, the controller.send
method is called. The send method can take one or more parameters. The first is the name of the action to be triggered. This is followed by any values that should be passed in the method.
ember server
and navigate to /tests
. This will display the passed tests: