我有我认为是 3 个非常简单的测试。
1) 检查组件渲染属性(Ember-CLI 自动生成此属性)
2)单击导航到“user.index”路由的类(它是{{link-to}})
3)单击导航到“brands.index”路线的类(它是{{link-to}})
当我在浏览器中单击它们时,我可以确认这些路线是可访问的,但是,测试失败了。尽管指定点击了“brands-link”,但“brands.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>