如何测试Ember.Router

How to test Ember.Router

我想测试我的路由器,为了简单起见,它看起来如下:

// app.router.js
import Ember from 'ember';
import config from './config/environment';

const Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.route('sessions', function() {
    this.route('login');
    this.route('logout');
  });
  this.route('profile');
});

export default Router;

是否可以对其进行单元测试? 我尝试使用验收测试但没有成功:

import Ember from 'ember';
import { test } from 'qunit';

import moduleForAcceptance from 'transformed-admin/tests/helpers/module-for-acceptance';

import startApp from 'transformed-admin/tests/helpers/start-app';

moduleForAcceptance('Acceptance | configuration', {
  beforeEach: function() {
    this.application = startApp();
  },

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

test('should map routes correctly', function(assert) {   
  visit('/');

  const app = this.application;

  andThen(function() {
    app.Router.detect("profile"); // false
    app.Router.detect("Profile"); // false

    const a = app.Router.extend({});
    a.detect("profile"); // false
    a.detect("Profile"); // false
  });
});

此处的最佳做法是什么?你测试过 Router.map() 吗?还是您依靠具体路线的测试来保证 Router.map() 是否正确写入?

不太确定你想做什么。如果您想确保它们可见,您可以为每条路线写 acceptance tests

import { test } from 'qunit';
import moduleForAcceptance from 'people/tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | login');

test('visiting /', function(assert) {
  visit('/');

  andThen(function() {
    assert.equal(currentURL(), '/index');
    assert.equal(currentPath(), 'index');
  });
});

test('visiting /profile', function(assert) {
  visit('/profile');

  andThen(function() {
    assert.equal(currentURL(), '/profile');
    assert.equal(currentPath(), 'profile');
  });
});

你也可以写成unit tests for your routes.

您不应测试 Ember.js 内部结构。 Ember.Routeris covered by tests。您应该测试您的应用程序特定逻辑(例如,通过单元测试处理路由中的特定操作)和行为(例如,通过验收测试存在特定路由)。