EmberJS Service Injection for Unit Tests (Ember QUnit)

纵饮孤独 提交于 2019-12-01 16:55:46

You don't have to import service. You have to include service in needs like below.

moduleFor("controller:test", {
   needs: ['service:alias']
});

For eg:

service / alias.js

Em.service.extend({
  name: 'john'
});

controllers / test.js

Em.Controller.extend({
  alias: Em.service.inject(),

  test: function() {
    alert(this.get('alias.name');
    }
  });

tests/unit/controllers/test-test.js

moduleFor('controller:test', {
  needs: ['service:store']
});

test('Alias Alias Alias', function(assert) {

    var controller = this.subject();
    assert.equal(controller.get('store.name'), 'john);

    });

For this test to run, Ember will generate a container with the controller test and service alias. So you can access the service properties with its name prefixed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!