Ember 简单的身份验证测试 window.location.reload

Ember Simple Auth testing window.location.reload

我已经让 ESA 与 Ember 2.0.1 一起很好地工作,但在测试时偶然发现了一个有趣的案例:

给出以下测试:

import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from 'notifier/tests/helpers/start-app';
import Pretender from 'pretender';
import { authenticateSession } from '../../helpers/ember-simple-auth';

let server;
let application;

module('Acceptance | signout', {
  beforeEach: function() {
    application = startApp();
  },

  afterEach: function() {
    Ember.run(application, 'destroy');
    server.shutdown();
  }
});

test('successfully sign out and get redirected', function(assert) {

  server = new Pretender(function() {
    this.post('/oauth/revoke', function() {
        return [200, {"Content-Type": "application/json"}];
    });
  });

  authenticateSession(application);
  visit('/admin');
  click('#sign-out');
  andThen(() => {
    assert.equal(currentRouteName(), 'users.sign-in');
  });
});

测试结果是路由永远不变。它保留在 /admin。这只发生在测试中,如果我手动与应用程序交互,它会正常工作。

发生这种情况的原因是在会话根据 https://github.com/simplabs/ember-simple-auth/blob/jj-abrams/addon/mixins/application-route-mixin.js#L99-L101.

无效后页面永远不会重新加载 (window.location.reload())

因此 AuthenticatedRouteMixin 中的 beforeModel hook 永远不会被触发,因此测试永远不会从 /admin 重定向到 /users/sign-in

我知道发生这种情况是因为您不能 运行 window.location.reload() 进行测试,但我不确定要使用什么替代方法。我可以在我的应用程序路由中覆盖 sessionInvalidated(),并在测试时将应用程序重定向到 /users/sign-in,但这不再是我想的实际测试应用程序。

有什么建议吗?

您实际上无法在测试模式下重新加载位置,因为那样会重新启动测试套件,从而导致无限循环。你可以用 sinon 存根它并断言存根被调用。