2

我在测试我的登录名和应用程序的相关功能时遇到了一些问题。该应用程序运行良好,但测试失败。对于测试,我使用Qunitwithkarma

我创建了一些经过身份验证的路由(比如帐户),只有在登录后才能访问。如果用户在没有登录的情况下转到帐户路由,他将被重定向到登录页面,并在成功登录后重定向回帐户页面。

App.AuthenticatedRoute = Ember.Route.extend({
  beforeModel: function(transition) {
    if (!App.AuthManager.isAuthenticated()) {
      this.redirectToLogin(transition);
    }
  },
  redirectToLogin: function(transition) {
    var loginController;
    loginController = this.controllerFor('login');
    loginController.set("attemptedTransition", transition);
    this.transitionTo("login");
  },
  events: {
    error: function(reason, transition) {
      this.redirectToLogin(transition);
    }
  }
});


App.LoginController = Ember.ObjectController.extend({
  attemptedTransition: null,
  loginUser: function() {
    var attemptedTran, form_data, router, that;
    router = this.get("target");
    form_data = this.getProperties("email", "password");
    attemptedTran = this.get('attemptedTransition');
    that = this;
    return $.post("/sessions", {
      'session': form_data
    }, (function(results) {
      return Ember.run(function() {
        App.AuthManager.authenticate(results.api_key.access_token, results.api_key.user_id);
        if (attemptedTran) {
          attemptedTran.retry();
          return that.set('attemptedTransition', null);
        } else {
          return router.transitionTo("index");
        }
      });
    }), "json");
  }
});

App.AccountsRoute = App.AuthenticatedRoute.extend({
  model: function() {
    return this.store.find('account');
  }
});

我正在尝试使用

test("account index", function() {
    expect(3); // Ensure that we will perform one assertion

  visit("/accounts").andThen(function() {
    equal(currentRouteName(),"login",'Accounts is an authenticated Route. so redirected to login page');
    fillIn('input[type=text]', "j@j.com");
    fillIn('input[type=password]', "jjjjjj");
    click("button:submit").andThen(function(){
      equal(currentRouteName(),"accounts",'After login redirected back to account page.');
    })
 });

但此测试在登录后失败,并且不会重定向回帐户页面。有什么帮助吗??

4

1 回答 1

0

看起来你正在设置previousTransition然后得到attemptedTransition. attemptedTransition根据上述逻辑,应始终为 null。

于 2014-05-29T16:17:27.083 回答