【问题标题】:Unknown `service:session` injector in Ember route unit testEmber路由单元测试中的未知`service:session`注入器
【发布时间】:2016-07-17 18:03:00
【问题描述】:

我的 Ember 应用中有简单的登录路径:

import Ember from 'ember';
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';

export default Ember.Route.extend(UnauthenticatedRouteMixin, {
});

并没有更复杂的单元测试:

import { moduleFor, test } from 'ember-qunit';

moduleFor('route:sign-in', 'Unit | Route | sign in', {
  needs: ['service:session']
});

test('it exists', function(assert) {
  let route = this.subject();
  assert.ok(route);
});

但是当运行ember test -f sign 时,它会失败并提示我无法注入service:session

Built project successfully. Stored in "/Users/hauleth/Workspace/hauleth/crossover/frontend/tmp/core_object-tests_dist-P16KQenq.tmp".
not ok 1 PhantomJS 2.1 - Unit | Controller | sign in: it exists
    ---
        actual: >
            null
        stack: >
            validateInjections@http://localhost:7357/assets/vendor.js:12932:91
            http://localhost:7357/assets/vendor.js:12107:48
            runInDebug@http://localhost:7357/assets/vendor.js:17219:9
            runInDebug@http://localhost:7357/assets/vendor.js:26382:43
            instantiate@http://localhost:7357/assets/vendor.js:12101:34
            lookup@http://localhost:7357/assets/vendor.js:11955:28
            buildInjections@http://localhost:7357/assets/vendor.js:11994:42
            injectionsFor@http://localhost:7357/assets/vendor.js:12067:37
            factoryFor@http://localhost:7357/assets/vendor.js:12038:37
            lookupFactory@http://localhost:7357/assets/vendor.js:11888:24
            factory@http://localhost:7357/assets/test-support.js:7816:39
            http://localhost:7357/assets/test-support.js:7904:75
            http://localhost:7357/assets/tests.js:345:34
            wrapper@http://localhost:7357/assets/test-support.js:6634:34
            runTest@http://localhost:7357/assets/test-support.js:2779:32
            run@http://localhost:7357/assets/test-support.js:2764:11
            http://localhost:7357/assets/test-support.js:2906:14
            process@http://localhost:7357/assets/test-support.js:2565:24
            begin@http://localhost:7357/assets/test-support.js:2547:9
            http://localhost:7357/assets/test-support.js:2607:9
        message: >
            Died on test #1 testWrapper@http://localhost:7357/assets/test-support.js:6663:16
            test@http://localhost:7357/assets/test-support.js:6676:44
            http://localhost:7357/assets/tests.js:344:24
            exports@http://localhost:7357/assets/vendor.js:131:37
            requireModule@http://localhost:7357/assets/vendor.js:30:25
            require@http://localhost:7357/assets/test-loader.js:67:16
            loadModules@http://localhost:7357/assets/test-loader.js:58:25
            load@http://localhost:7357/assets/test-loader.js:89:35
            http://localhost:7357/assets/test-support.js:6485:20: Attempting to inject an unknown injection: `service:session`
        Log: |
    ...
ok 2 PhantomJS 2.1 - Unit | Route | sign in: it exists

1..2
# tests 2
# pass  1
# skip  0
# fail  1

我试图找到解决方案,但我找到的都是关于集成测试或告诉我添加needs: ['service:session'],但它已经存在了。

【问题讨论】:

    标签: unit-testing ember.js dependency-injection qunit ember-simple-auth


    【解决方案1】:

    我也遇到过。我认为这是因为在您的应用程序中找不到session 服务(它在插件中)。不过不要相信我的话。

    无论如何,您可以做的是将会话对象本身存根在路由主题中而不是在需要中。

    例如

    test('it exists', function(assert) {
      let route = this.subject({session: Ember.Object.create()});
      assert.ok(route);
    });
    

    如果您在会话中需要 session.get('isAuthenticated') 之类的东西,那么问题是它会失败,因为您只是简单地将空的 session 存根。所以你应该像这样存根它:

    let route = this.subject({session: Ember.Object.create({isAuthenticated: true});
    

    提示:创建一个测试助手来创建会话存根会很有用。

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 2021-04-26
      • 2015-04-01
      • 2022-06-23
      • 2014-03-28
      • 2016-11-23
      相关资源
      最近更新 更多