home
  • Blog
1.10
  • Getting Started
  • Getting Ember
  • Concepts
  • The Object Model
  • Application
  • Templates
  • Routing
  • Components
  • Controllers
  • Models
  • Views
  • Enumerables
  • Testing
    • Introduction
    • Integration Tests
    • Test Helpers
    • Testing User Interaction
    • Unit Testing Basics
    • Unit Test Helpers
    • Testing Components
    • Testing Controllers
    • Testing Routes
    • Testing Models
    • Automating Tests with Runners
  • Configuring Ember.js
  • Cookbook
  • Understanding Ember.js
  • Contributing to Ember.js
Old Guides - You are viewing the guides for Ember v1.10.0.
Go to v5.0.0

Unit Testing Basics

Edit pencil

Unit tests are generally used to test a small piece of code and ensure that it is doing what was intended. Unlike integration tests, they are narrow in scope and do not require the Ember application to be running.

As it is the basic object type in Ember, being able to test a simple Ember.Object sets the foundation for testing more specific parts of your Ember application such as controllers, components, etc. Testing an Ember.Object is as simple as creating an instance of the object, setting its state, and running assertions against the object. By way of example lets look at a few common cases.

Testing Computed Properties

Let's start by looking at an object that has a computedFoo computed property based on a foo property.

App.SomeThing = Ember.Object.extend({
  foo: 'bar',
  computedFoo: function(){
    return 'computed ' + this.get('foo');
  }.property('foo')
});

Within the test we'll create an instance, update the foo property (which should trigger the computed property), and assert that the logic in our computed property is working correctly.

module('Unit: SomeThing');

test('computedFoo correctly concats foo', function() {
  var someThing = App.SomeThing.create();
  someThing.set('foo', 'baz');
  equal(someThing.get('computedFoo'), 'computed baz');
});

Live Example

Unit Testing Basics: Computed Properties

Testing Object Methods

Next let's look at testing logic found within an object's method. In this case the testMethod method alters some internal state of the object (by updating the foo property).

App.SomeThing = Ember.Object.extend({
  foo: 'bar',
  testMethod: function() {
    this.set('foo', 'baz');
  }
});

To test it, we create an instance of our class SomeThing as defined above, call the testMethod method and assert that the internal state is correct as a result of the method call.

module('Unit: SomeThing');

test('calling testMethod updates foo', function() {
  var someThing = App.SomeThing.create();
  someThing.testMethod();
  equal(someThing.get('foo'), 'baz');
});

Live Example

Unit Testing Basics: Method Side Effects

In the event the object's method returns a value you can simply assert that the return value is calculated correctly. Suppose our object has a calc method that returns a value based on some internal state.

App.SomeThing = Ember.Object.extend({
  count: 0,
  calc: function() {
    this.incrementProperty('count');
    return 'count: ' + this.get('count');
  }
});

The test would call the calc method and assert it gets back the correct value.

module('Unit: SomeThing');

test('testMethod returns incremented count', function() {
  var someThing = App.SomeThing.create();
  equal(someThing.calc(), 'count: 1');
  equal(someThing.calc(), 'count: 2');
});

Live Example

Unit Testing Basics: Method Side Effects

Testing Observers

Suppose we have an object that has a property and a method observing that property.

App.SomeThing = Ember.Object.extend({
  foo: 'bar',
  other: 'no',
  doSomething: function(){
    this.set('other', 'yes');
  }.observes('foo')
});

In order to test the doSomething method we create an instance of SomeThing, update the observed property (foo), and assert that the expected effects are present.

module('Unit: SomeThing');

test('doSomething observer sets other prop', function() {
  var someThing = App.SomeThing.create();
  someThing.set('foo', 'baz');
  equal(someThing.get('other'), 'yes');
});

Live Example

Unit Testing Basics: Observers

left arrow
Testing User Interaction
Unit Test Helpers
right arrow
On this page

  • Testing Computed Properties
  • Live Example
  • Testing Object Methods
  • Live Example
  • Live Example
  • Testing Observers
  • Live Example
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2023 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue