Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ember unit testing


May 09, 2021 Ember.js Reference documents


Table of contents


The basis for unit testing

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.

Test the calculated property

Create a simple instance that contains a calculated computedFoo relies on the normal property foot

  1. //app/models/somt-thing.js
  2. export default Ember.Object.extend({
  3. foo: 'bar',
  4. computedFoo: Ember.compuuted('foo'function() {
  5. const foo = this.get('foo');
  6. return `computed ${foo}`;
  7. })
  8. });

In the test, we build an instance, then update the foo (which computedFoo and then give an assertion that is in line with 断言 :

  1. //tests/unit/models/some-thing-test.js
  2. import {moduleFor test} from 'ember-qunit';
  3. moduleFor('model:some-thing' 'Unit | some thing' {
  4. unit: true
  5. });
  6. test('should correctly concat foo' function(assert) {
  7. const someThing = this.subject();
  8. somtThing.set('foo' 'baz'); //设置属性foo的值
  9. assert.equal(someThing.get('computedFoo'), 'computed baz'); //断言,判断计算属性值是否相等于computed baz
  10. });

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

Test the object method

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

  1. //app/models/some-thing.js
  2. export default Ember.Object.extend({
  3. foo: 'bar'
  4. testMethod() {
  5. this.set('foo', 'baz');
  6. }
  7. });

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.

  1. //tests/unit/models/some-thing-test.js
  2. test('should update foo on testMethod' function(assert) {
  3. const someThing = this.subject();
  4. someThing.testMethod();
  5. assert.equal(someThing.get('foo'), 'baz');
  6. });

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:

  1. //app/models/some-thing.js
  2. export default Ember.Object.extend({
  3. count: 0,
  4. calc() {
  5. this.incrementProperty('count');
  6. let count = this.get('count');
  7. return `count: ${count}`;
  8. }
  9. });

The calc method needs to calc in the test and the return value is asserted to be correct.

  1. //tests/unit/models/some-thing-test.js
  2. test('should return incremented count on calc' function(assert) {
  3. const someThing = this.subject();
  4. assert.equal(someThing.calc(), 'count: 1');
  5. assert.equal(someThing.calc(), 'count: 2');
  6. });

Test the observer

Suppose we have an object that has some properties and a method that monitors them.

  1. //app/models/some-thing.js
  2. export default Ember.Object.extend({
  3. foo: 'bar'
  4. other: 'no',,
  5. doSomething: Ember.observer('foo', function() {
  6. this.set('other', 'yes');
  7. })
  8. });

To test doSomething create a SomeThing foo property value, and then make an assertion that the expected results are achieved.

  1. //tests/unit/models/some-thing-test.js
  2. test('should set other prop to yes when foo changes' function(assert) {
  3. const someThing = this.subject();
  4. someThing.set('foo', 'baz');
  5. assert.equal(someThing.get('other'), 'yes');
  6. });