首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >组件测试Ember-带有{{link-to}}帮助器的CLI

组件测试Ember-带有{{link-to}}帮助器的CLI
EN

Stack Overflow用户
提问于 2014-11-22 17:55:52
回答 2查看 758关注 0票数 2

我有我认为是3个非常简单的测试。

1)检查组件渲染属性(Ember-CLI自动生成此属性)

2)单击导航到'user.index‘路径的类(它是{{link- to }})

3)单击导航到'brands.index‘路径的类(它是{{link- to }})

当我在浏览器中单击路由时,我可以确认它们是可访问的,但是,测试失败。尽管指定单击了“brands.index-link”,“users.index”测试仍然期望“users.index”。

任何帮助都将不胜感激!

测试如下:

代码语言:javascript
运行
复制
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');
  });
});

组件模板为:

代码语言:javascript
运行
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2015-02-16 00:06:04

我相信这是因为当你使用moduleForComponent助手时,你的Ember应用程序并没有启动。link-to帮助器需要路由器,路由器不会存在,除非应用程序真正启动(即使用常规的module帮助器并在beforeEach块中调用startApp() )。

我不确定最好的解决方案是什么。您可以对使用此组件的路由进行正常的集成测试,但这似乎相当笨拙。

票数 2
EN

Stack Overflow用户

发布于 2015-02-16 01:35:39

问题是,您试图在一个应该进行单元测试的文件中进行集成测试。请阅读:http://emberjs.com/guides/testing/

对于集成测试,您需要执行以下操作:

代码语言:javascript
运行
复制
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() {
});

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27076092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档