【问题标题】:How do you inject the session for a unit test in ember-simple-auth?如何在 ember-simple-auth 中注入会话以进行单元测试?
【发布时间】:2016-01-20 14:02:27
【问题描述】:

我无法在我的 ember 应用程序中为应用程序路由设置单元测试。

很简单的路线:

import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

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

非常简单的测试:

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


import { authenticateSession } from '../../helpers/ember-simple-auth';

moduleFor('route:application', {
    beforeEach: function(){
        this.subject().set('session', authenticateSession);
    },
});

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

我尝试了几件事,但都抛出了错误:

Promise rejected before it exists: 'undefined' is not an object (evaluating '_this.get('session').on')

这似乎表明会话在 mixin 中未定义,有人有单元测试工作吗?我可以转换为集成测试,但如果可能,我更愿意将其保留为单元级别。

【问题讨论】:

  • 在您的路线中尝试过session: inject.service(),
  • 查看了源代码,这就是 mixin 中发生的事情,无论如何都尝试过,但没有任何影响。好主意。

标签: unit-testing ember.js ember-simple-auth


【解决方案1】:

万一其他人遇到这种情况,这只是一个“需要:['service:session']”,正如在此处的不同答案中找到的那样:

EmberJS Service Injection for Unit Tests (Ember QUnit)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    相关资源
    最近更新 更多