2

我有我认为是 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>
4

2 回答 2

2

我相信这是因为当您使用moduleForComponent帮助程序时,您没有启动您的 Ember 应用程序。助手需要路由器,除非应用程序实际启动(即使用常规助手并在块中调用),否则路由器将link-to不存在或正确设置。modulestartApp()beforeEach

我不确定这里的最佳解决方案...您可以对使用此组件的路由进行正常的集成测试,但这似乎很尴尬。

于 2015-02-15T16:06:03.870 回答
0

问题是您试图在一个旨在进行单元测试的文件中进行集成测试。阅读: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() {
});

根据库的版本,代码需要调整。

于 2015-02-15T17:35:39.290 回答