【问题标题】:Ember initializer async property undefined in integration test集成测试中未定义的 Ember 初始化程序异步属性
【发布时间】:2014-11-12 23:41:15
【问题描述】:

我正在尝试测试我的注册和登录过程,集成测试在创建初始化程序以使用 currentUser 属性扩展 Ember-Simple-Auth Session 对象之前完美通过。

这一切都在浏览器中正常工作,只是测试现在在以下行的应用程序路由中的 sessionAuthenticationSucceeded 操作中全部失败:

this.get('session.currentUser').then(function(user) {

with : TypeError: Cannot read property 'then' of undefined


/routes/application.js

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

export default Ember.Route.extend(ApplicationRouteMixin, {
  actions: {
    sessionAuthenticationSucceeded: function () {
      var self = this;
      this.get('session.currentUser').then(function(user) {
          if (user.get('account') && user.get('status') === 'complete'){
            self.transtionTo('home');
          } else {
            console.log('Need to complete Registration');
            self.transitionTo('me');
          }
      });
    }
  }
}

/initializers/custom-session.js

import Ember from 'ember';
import Session from 'simple-auth/session';

export default {
  name: 'custom-session',
  before: 'simple-auth',
  initialize: function(container) {
    // application.deferReadiness();
    Session.reopen({
      currentUser: function() {
        var id = this.get('user_id');
        if (!Ember.isEmpty(id)) {
          console.log('getting the current user');
          return container.lookup('store:main').find('user', id);
        }
      }.property('user_id')
    });
    // application.advanceReadiness();
  }
};

/tests/integration/visitor-signs-up-test.js

test('As a user with valid email and password', function(){
  var email = faker.internet.email();
  signUpUser(email, 'correctpassword', 'correctpassword');
  andThen(function(){
    equal(find('#logged-in-user').text(), email, 'User registered successfully as ' + email);
    equal(sessionIsAuthenticated(App), true, 'The session is Authenticated');
  });
});

test/helpers/registration-login.js

export function signUpUser(email, password, passwordConfirmation) {
  visit('/register').then(function(){
    fillIn('input.email', email);
    fillIn('input.password', password);
    fillIn('input.password-confirmation', passwordConfirmation);
    click('button.submit');
  });
}

我尝试过使用

application.deferReadiness()

正如您在初始化程序中看到的那样(也在该实例中传入应用程序)以确保异步请求已完成并且用户可用,但这也不起作用。

我正在使用Pretender 拦截api 请求,但在测试期间根本没有调用api/v1/users/:id

奇怪的是它可以在浏览器中完美运行。

我试图理解为什么这不起作用?任何指导将不胜感激!

注意:我还尝试了herehere 列出的解决方案,结果与上述相同。

【问题讨论】:

    标签: ember.js integration-testing ember-cli ember-simple-auth


    【解决方案1】:

    我已经找到了问题所在,结果我没有从api/v1/users/sign_in 请求中返回user_id Pretender 正在拦截,因此当sessionAuthenticationSucceeded 被触发时,没有user_id 可用,因此currentUser 是永远不会被更新/触发。

    我会将所有代码保留在那里,以防它帮助其他人。仍然非常欢迎对其提出意见或改进!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      相关资源
      最近更新 更多