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.

尽管Ember.Object是Ember中得基础对象类型,然后能够测试一个简单的Ember.Object是能测试Ember应用各个特定部分的基础。如控制器、组件等。测试Ember.Object就如创建一个对象实例一样简单,设置其状态并针对对象进行断言。作为实例,下面将来介绍一些一般性的场景。

Testing Computed Properties

测试计算属性

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

下面从一个具有基于foo属性的computedFoo计算属性的对象开始。

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

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.

在测试中,创建了一个实例,更新实例的foo属性(这将触发计算属性),然后断言计算属性中得逻辑工作正常。

  1. module('Unit: SomeThing');
  2. test('computedFoo correctly concats foo', function() {
  3. var someThing = App.SomeThing.create();
  4. someThing.set('foo', 'baz');
  5. equal(someThing.get('computedFoo'), 'computed baz');
  6. });

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).

下面再看一个测试对象方法逻辑的例子。在这里testMethod方法修改了对象的一些内部状态(更新了foo属性)。

  1. App.SomeThing = Ember.Object.extend({
  2. foo: 'bar',
  3. testMethod: function() {
  4. this.set('foo', 'baz');
  5. }
  6. });

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.

为了测试这个方法,首先创建了一个SomeThing的实例,调用其testMethod,然后断言内部状态与期望的一样。

  1. module('Unit: SomeThing');
  2. test('calling testMethod updates foo', function() {
  3. var someThing = App.SomeThing.create();
  4. someThing.testMethod();
  5. equal(someThing.get('foo'), 'baz');
  6. });

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.

在这里对象的方法返回了一个值,可以通过简单的断言来判断值是否被正确计算。假设对象有一个calc方法,其基于一些内部的状态返回一个值。

  1. App.SomeThing = Ember.Object.extend({
  2. count: 0,
  3. calc: function() {
  4. this.incrementProperty('count');
  5. return 'count: ' + this.get('count');
  6. }
  7. });

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

测试要调用calc方法并断言其返回正确的值。

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

Live Example

在线示例

Unit Testing Basics: Method Side Effects

单元测试基础:方法负效应

Testing Observers

测试观察器

Suppose we have an object that has an observable method based on the foo property.

假设有一个对象其有一个观察foo属性的方法。

  1. App.SomeThing = Ember.Object.extend({
  2. foo: 'bar',
  3. other: 'no',
  4. doSomething: function(){
  5. this.set('other', 'yes');
  6. }.observes('foo')
  7. });

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.

为了测试doSomething方法,首先创建一个SomeThing实例,然后修改被观察的属性(foo),并断言期待的效果出现。

  1. module('Unit: SomeThing');
  2. test('doSomething observer sets other prop', function() {
  3. var someThing = App.SomeThing.create();
  4. someThing.set('foo', 'baz');
  5. equal(someThing.get('other'), 'yes');
  6. });

Live Example

在线示例

Unit Testing Basics: Observers

单元测试基础:观察器