我有我认为是3个非常简单的测试。
1)检查组件渲染属性(Ember-CLI自动生成此属性)
2)单击导航到'user.index‘路径的类(它是{{link- to }})
3)单击导航到'brands.index‘路径的类(它是{{link- to }})
当我在浏览器中单击路由时,我可以确认它们是可访问的,但是,测试失败。尽管指定单击了“brands.index-link”,“users.index”测试仍然期望“users.index”。
任何帮助都将不胜感激!
测试如下:
import {
moduleForComponent,
test
} from 'ember-qunit';
moduleForComponent('navigation-bar', 'NavigationBarComponent', {
// specify the other units that are required for this test
// needs: ['component:foo', 'helper:bar']
});
test('it renders', function () {
expect(2);
// creates the component instance
var component = this.subject();
equal(component._state, 'preRender');
// appends the component to the page
this.append();
equal(component._state, 'inDOM');
});
test('it can navigate to users', function () {
expect(3);
var component = this.subject();
equal(component._state, 'preRender');
this.append();
equal(component._state, 'inDOM');
click('.users-link');
andThen(function () {
equal(currentRouteName(), 'users.index');
});
});
test('it can navigate to brands', function () {
expect(3);
var component = this.subject();
equal(component._state, 'preRender');
this.append();
equal(component._state, 'inDOM');
click('.brands-link');
andThen(function () {
equal(currentRouteName(), 'brands.index');
});
});
组件模板为:
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">
<!--<img alt="Brand" src="...">-->
</a>
</div>
<ul class="nav navbar-nav">
{{#link-to 'users' tagName='li' classNames='users-link'}}<a href="#">Users</a>{{/link-to}}
{{#link-to 'brands' tagName='li' classNames='brands-link'}}<a href="#">Brands</a>{{/link-to}}
</ul>
</div>
</nav>
发布于 2015-02-16 00:06:04
我相信这是因为当你使用moduleForComponent
助手时,你的Ember应用程序并没有启动。link-to
帮助器需要路由器,路由器不会存在,除非应用程序真正启动(即使用常规的module
帮助器并在beforeEach
块中调用startApp()
)。
我不确定最好的解决方案是什么。您可以对使用此组件的路由进行正常的集成测试,但这似乎相当笨拙。
发布于 2015-02-16 01:35:39
问题是,您试图在一个应该进行单元测试的文件中进行集成测试。请阅读:http://emberjs.com/guides/testing/
对于集成测试,您需要执行以下操作:
import Ember from 'ember';
import startApp from '../helpers/start-app';
var App, server;
module('My First Integration Test', {
setup: function() {
App = startApp();
},
teardown: function() {
Ember.run(App, 'destroy');
server.shutdown();
}
});
test('test something', function() {
});
根据您的库的版本,代码将需要调整。
https://stackoverflow.com/questions/27076092
复制相似问题