- EmberJS Tutorial
- EmberJS - Home
- EmberJS - Overview
- EmberJS - Installation
- EmberJS - Core Concepts
- Creating and Running Application
- EmberJS - Object Model
- EmberJS - Router
- EmberJS - Templates
- EmberJS - Components
- EmberJS - Models
- EmberJS - Managing Dependencies
- EmberJS - Application Concerns
- EmberJS - Configuring Ember.js
- EmberJS - Ember Inspector
- EmberJS Useful Resources
- EmberJS - Quick Guide
- EmberJS - Useful Resources
- EmberJS - Discussion
EmberJS-Testing Models
Testing Models
In Ember, every route has an model which is set by implementing a route's 'model' hook. The models of Ember can be tested using the moduleForModel helper.
Example
<!DOCTYPE html> <html> <head> <title>EmberJs Tesing Models</title> <link href="https://code.jquery.com/qunit/qunit-git.css" rel="stylesheet" type="text/css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.prod.js"></script> <script src="https://code.jquery.com/qunit/qunit-1.18.0.js"></script> <script src="https://rawgit.com/rwjblue/ember-qunit-builds/master/ember-qunit.js"></script> <script src="https://builds.emberjs.com/release/ember.debug.js"></script> <script src="https://builds.emberjs.com/beta/ember-data.js"></script> </head> <body> <div id="qunit"> </div> <div id="ember-testing"></div> <script type="text/javascript"> //Creates an instance of Ember.Application and assign it to a global variable App = Ember.Application.create(); //Create a subclass of 'DS.Model' for 'Employee' model in the application App.Employee = DS.Model.extend({ //Specify which attributes a model has by using 'DS.attr' Empno: DS.attr('number', { defaultNum: 0 }), EmpName: DS.attr('string', { defaultName: 'Mack' }), //Create a test which will call 'EmpSalary' on the player when they are level 4 to assert that the 'EmpName' changes EmpSalary: function() { var Eno = this.incrementProperty('Empno'); if (Eno === 5) { this.set('EmpName', 'Manu'); } } }); //These methods are used to prepare the application for testing //emq.globalize(); App.setupForTesting(); App.injectTestHelpers(); //The 'DefaultResolver' defines the default lookup rules before consulting the container for registered items setResolver(Ember.DefaultResolver.create({ namespace: App })); App.rootElement = '#ember-testing'; //Ember.js applications's root element //The 'moduleFor' helper is used to setup a test container moduleForModel('employee', 'Employee Model'); test('EmpSalary', function() { var employee = this.subject({ Empno: 4 }); //This is an async function and should be wrapped using 'Ember.run' Ember.run(function() { employee.EmpSalary(); }); //Check the values before we modify the post equal(employee.get('Empno'), 5); equal(employee.get('EmpName'), 'Manu'); }); </script> </body> </html>
Output
Let's carry out the following steps to see how above code works −
Save above code in testing_models.htm file
Open this HTML file in a browser.
Advertisements
To Continue Learning Please Login
Login with Google