为了简单起见,我想测试我的路由器,如下所示:
// 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()
的编写正确?
不太确定你想做什么。如果你想确保每条路线都是可见的,你可以为它们编写验收测试:
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');
});
});
您也可以为您的路由编写单元测试。
您不应该测试Ember.js内部。Ember.Router包含在测试中。您应该测试您的应用程序特定逻辑(例如,通过单元测试处理路由中的特定操作)和行为(例如,特定路由通过验收测试存在)。