May 09, 2021 Ember.js Reference documents
Unit tests are typically used to test small blocks of code and make sure what it is doing. U nlike acceptance tests, unit tests are limited to a small scale and do not require the Emeber program to run.
As with Ember's basic objects, you only need to inherit
Ember.Object
T
hen write specific tests within the code block, such as controllers and components. E
ach test is an
Ember.Object
and you can set the state of the object and run assertions.
With the following example, let's take a look at how the test is used.
Create a simple instance that contains a calculated
computedFoo
relies on the normal property
foot
//app/models/somt-thing.js
export default Ember.Object.extend({
foo: 'bar',
computedFoo: Ember.compuuted('foo',function() {
const foo = this.get('foo');
return `computed ${foo}`;
})
});
In the test, we build an instance, then update the
foo
(which
computedFoo
and then give an assertion that is in line with
断言
:
//tests/unit/models/some-thing-test.js
import {moduleFor, test} from 'ember-qunit';
moduleFor('model:some-thing', 'Unit | some thing', {
unit: true
});
test('should correctly concat foo', function(assert) {
const someThing = this.subject();
somtThing.set('foo', 'baz'); //设置属性foo的值
assert.equal(someThing.get('computedFoo'), 'computed baz'); //断言,判断计算属性值是否相等于computed baz
});
The moduleFor is
moduleFor
example, which is a unit test assistant
Ember-Qunit
T
hese test assistants provide us with many conveniences, such as
subject
which can find and instantiate the objects used for testing. Y
ou can also
subject
initialized content in the subject method, which can be instance variables passed to the principal functionality. F
or example, in unit tests to initialize the property "foo" you can do this:
this.subject({foo: 'bar'});
, then the value of the unit test's
foo
is
bar
Let's take a look at how to test the logic of the object method.
In this case, there is a method inside the object that sets the value of the
foo
the property
testMethod
//app/models/some-thing.js
export default Ember.Object.extend({
foo: 'bar',
testMethod() {
this.set('foo', 'baz');
}
});
To test it, we first create the following instance, then call
testMethod
and then use assertions to determine whether the method's call results are correct.
//tests/unit/models/some-thing-test.js
test('should update foo on testMethod', function(assert) {
const someThing = this.subject();
someThing.testMethod();
assert.equal(someThing.get('foo'), 'baz');
});
If an object method returns a value, you can easily give an assertion to determine whether it is correct. S
uppose our object has a
calc
the return value of the method is based on the state value inside the object.
The code is as follows:
//app/models/some-thing.js
export default Ember.Object.extend({
count: 0,
calc() {
this.incrementProperty('count');
let count = this.get('count');
return `count: ${count}`;
}
});
The calc method needs to
calc
in the test and the return value is asserted to be correct.
//tests/unit/models/some-thing-test.js
test('should return incremented count on calc', function(assert) {
const someThing = this.subject();
assert.equal(someThing.calc(), 'count: 1');
assert.equal(someThing.calc(), 'count: 2');
});
Suppose we have an object that has some properties and a method that monitors them.
//app/models/some-thing.js
export default Ember.Object.extend({
foo: 'bar'
other: 'no',,
doSomething: Ember.observer('foo', function() {
this.set('other', 'yes');
})
});
To test
doSomething
create a
SomeThing
foo
property value, and then make an assertion that the expected results are achieved.
//tests/unit/models/some-thing-test.js
test('should set other prop to yes when foo changes', function(assert) {
const someThing = this.subject();
someThing.set('foo', 'baz');
assert.equal(someThing.get('other'), 'yes');
});