Ember组件集成测试:`link-to` href为空

时间:2015-08-21 01:01:04

标签: javascript ember.js integration-testing htmlbars ember.js-helper

我尝试编写组件集成测试la this blog post,但我的组件对动态路由有link-to,而href属性不是{{#link-to "myModel" model}} 。正在填写。这是我试图做的简化版本。

我的组件模板:

this.set('model', {
  id: 'myId',
  name: 'My Name'
});

this.render(hbs`
{{my-component model=model}}
`);

assert.equal(this.$('a').attr('href'), '/myModel/myId'); // fails

以下是我测试的相关部分:

link-to

呈现href,只是没有<a id="ember283" class="ember-view">My Name</a> 属性。如果我在测试中记录HTML,它看起来像:

link-to

我需要做些什么来对待我的&#34;模型&#34;让link-to得到一个href?我试着在ember中查看id的测试,并找到this part of the tests,这基本上就是我正在做的事情 - 提供一个带有DEBUG: ------------------------------- DEBUG: Ember : 1.13.8 DEBUG: Ember Data : 1.13.10 DEBUG: jQuery : 1.11.3 DEBUG: ------------------------------- 密钥集的POJO。有什么想法吗?

修改

var iisLog = W3CEnumerable.FromFile(pathToLog);
int nbOfLogsForLastHour = iisLog.Where(x => x.dateTime > DateTime.Now.AddHours(-1)).Count();

4 个答案:

答案 0 :(得分:11)

事实证明,您只需查看路由器并告诉它在您的测试设置中开始路由,它就能正常工作。来自@rwjblue的this commit

// tests/helpers/setup-router.js

export default function({ container }) {
  const router = container.lookup('router:main');
  router.startRouting(true);
}


// tests/integration/components/my-component-test.js

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import setupRouter from '../../helpers/setup-router';

moduleForComponent('my-component', 'Integration | Component | my component', {
  integration: true,
  setup() {
    setupRouter(this);
  }
});

答案 1 :(得分:4)

如果您只想检查href是否正常,最好使用setupRouter代替startRoutingstartRouting尝试处理初始URL的初始转换,这可能会有问题。

// tests/helpers/setup-router.js
export default function({ container }) {
const router = container.lookup('router:main');
   router.setupRouter();
}

https://github.com/emberjs/ember.js/blob/d487061228a966d8aac6fa94a8d69abfc3f1f257/packages/ember-routing/lib/system/router.js#L202

答案 2 :(得分:3)

这是在Ember> 3中进行的操作

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Component | my-component', function (hooks) {
  setupRenderingTest(hooks);

  test('it has a link', async function (assert) {
    this.owner.lookup('router:main').setupRouter();
    await render(hbs`{{my-component}}`);
    assert.equal(this.element.querySelector('a').getAttribute('href'), 'some-url');
  });
});

答案 3 :(得分:0)

您使用的是什么版本的Ember?我记得以前看到这个,现在它似乎在我的应用程序中工作(虽然我使用的是1.13.8)。

似乎是在我的应用程序中添加了href,并且基于this computed property,如果tagName是“a”,它应该绑定到视图。

除了升级Ember之外,如果它仍然存在,最好创建一个余烬小提琴或复制品并提交错误。

您可能还想查看https://github.com/intercom/ember-href-to