如何测试Ember.Router

时间:2016-04-08 07:55:28

标签: testing ember.js 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()被正确写入?

1 个答案:

答案 0 :(得分:0)

不太确定你想做什么。如果您想确保它们可见,您可以为每条路线写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.Router is covered by tests。您应该测试您的应用程序特定逻辑(例如,通过单元测试处理路径中的特定操作)和行为(例如,通过验收测试存在特定路径)。