单元测试基础 编辑页面
英文原文:http://emberjs.com/guides/testing/unit-testing-basics/
尽管Ember.Object是Ember中得基础对象类型,然后能够测试一个简单的Ember.Object是能测试Ember应用各个特定部分的基础。如控制器、组件等。测试Ember.Object就如创建一个对象实例一样简单,设置其状态并针对对象进行断言。作为实例,下面将来介绍一些一般性的场景。
测试计算属性
下面从一个具有基于foo属性的computedFoo计算属性的对象开始。
1 2 3 4 5 6 |
App.SomeThing = Ember.Object.extend({
foo: 'bar',
computedFoo: function(){
return 'computed ' + this.get('foo');
}.property('foo')
});
|
在测试中,创建了一个实例,更新实例的foo属性(这将触发计算属性),然后断言计算属性中得逻辑工作正常。
1 2 3 4 5 6 7 |
module('Unit: SomeThing'); test('computedFoo correctly concats foo', function() { var someThing = App.SomeThing.create(); someThing.set('foo', 'baz'); equal(someThing.get('computedFoo'), 'computed baz'); }); |
在线示例
测试对象方法
下面再看一个测试对象方法逻辑的例子。在这里testMethod方法修改了对象的一些内部状态(更新了foo属性)。
1 2 3 4 5 6 |
App.SomeThing = Ember.Object.extend({
foo: 'bar',
testMethod: function() {
this.set('foo', 'baz');
}
});
|
为了测试这个方法,首先创建了一个SomeThing的实例,调用其testMethod,然后断言内部状态与期望的一样。
1 2 3 4 5 6 7 |
module('Unit: SomeThing'); test('calling testMethod updates foo', function() { var someThing = App.SomeThing.create(); someThing.testMethod(); equal(someThing.get('foo'), 'baz'); }); |
在线示例
在这里对象的方法返回了一个值,可以通过简单的断言来判断值是否被正确计算。假设对象有一个calc方法,其基于一些内部的状态返回一个值。
1 2 3 4 5 6 7 |
App.SomeThing = Ember.Object.extend({
count: 0,
calc: function() {
this.incrementProperty('count');
return 'count: ' + this.get('count');
}
});
|
测试要调用calc方法并断言其返回正确的值。
1 2 3 4 5 6 7 |
module('Unit: SomeThing'); test('testMethod returns incremented count', function() { var someThing = App.SomeThing.create(); equal(someThing.calc(), 'count: 1'); equal(someThing.calc(), 'count: 2'); }); |
在线示例
测试观察器
假设有一个对象其有一个观察foo属性的方法。
1 2 3 4 5 6 7 |
App.SomeThing = Ember.Object.extend({
foo: 'bar',
other: 'no',
doSomething: function(){
this.set('other', 'yes');
}.observes('foo')
});
|
为了测试doSomething方法,首先创建一个SomeThing实例,然后修改被观察的属性(foo),并断言期待的效果出现。
1 2 3 4 5 6 7 |
module('Unit: SomeThing'); test('doSomething observer sets other prop', function() { var someThing = App.SomeThing.create(); someThing.set('foo', 'baz'); equal(someThing.get('other'), 'yes'); }); |
Fork Us!
