Ember Simple Auth 1.0测试助手

时间:2016-03-17 09:16:18

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

我最近从0.8升级到1.0,我的应用程序运行正常。

有一件事让我感到惊讶,我仍然不明白应该如何使用新的验收测试助手。

之前(0.8)我可以像这样写我的测试,他们会通过:

test('sign in and sign out', function(assert) {
  visit('/');

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });

  authenticateSession();

  andThen(function() {
    assert.ok(find(':contains("Sign Out")').length,
      'expected to see "Sign Out"');
  });

  invalidateSession();

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });
});

但是,在升级并以新格式重写它们之后:

import { authenticateSession, invalidateSession } from 'instatube-app/tests/helpers/ember-simple-auth';

test('sign in and sign out', function(assert) {
  visit('/');

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });

  authenticateSession(application);

  andThen(function() {
    assert.ok(find(':contains("Sign Out")').length,
      'expected to see "Sign Out"');
  });

  invalidateSession(application);

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });
});

现在只有第一个断言通过了。

如果我将它们分成单独的测试,即:

test('when signed out display sign in button', function(assert) {
  visit('/');

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });

});

test('when signed in display sign out button', function(assert) {
  authenticateSession(application);
  visit('/');

  andThen(function() {
    assert.ok(find(':contains("Sign Out")').length,
      'expected to see "Sign Out"');
  });
});

然后这些单独的测试通过,但我似乎无法使它们适用于实际用例。

为什么会发生这种情况以及如何解决这个问题,我们将非常感激。

1 个答案:

答案 0 :(得分:1)

所以如果有其他人遇到这个问题,我设法通过在andThen服务员中包含帮助器来解决它,如下所示:

import { authenticateSession, invalidateSession } from 'instatube-app/tests/helpers/ember-simple-auth';

test('sign in and sign out', function(assert) {
  visit('/');

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
    authenticateSession(application);
  });

  andThen(function() {
    assert.ok(find(':contains("Sign Out")').length,
      'expected to see "Sign Out"');
    invalidateSession(application);
  });

  andThen(function() {
    assert.ok(find(':contains("Sign In")').length,
      'expected to see "Sign In"');
  });
});