gpt4 book ai didi

ember.js - 通过 add on 安装 Ember helper 然后在组件中使用它会破坏测试

转载 作者:行者123 更新时间:2023-12-01 12:36:17 25 4
gpt4 key购买 nike

我正在安装“ember-moment”助手,然后在组件中使用它。一旦我这样做,那些组件的测试就会中断,几乎什么也没说:

not ok 120 PhantomJS 1.9 - StatusCardComponent: it renders
---
actual: >
null
message: >
Died on test #2 at http://localhost:7357/assets/test-support.js:2779
at test (http://localhost:7357/assets/test-support.js:1796)
at http://localhost:7357/assets/client.js:11190
at http://localhost:7357/assets/vendor.js:150
at tryFinally (http://localhost:7357/assets/vendor.js:30)
at http://localhost:7357/assets/vendor.js:156
at http://localhost:7357/assets/test-loader.js:29
at http://localhost:7357/assets/test-loader.js:21
at http://localhost:7357/assets/test-loader.js:40
at http://localhost:7357/assets/test-support.js:5545: Assertion Failed: A helper named 'moment' could not be found
Log: >
...

这里是测试本身的代码,它只是自动生成:

import {
moduleForComponent,
test
} from 'ember-qunit';

moduleForComponent('event-card', {
// Specify the other units that are required for this test
// needs: ['component:foo', 'helper:bar']
});

test('it renders', function(assert) {
assert.expect(2);

// Creates the component instance
var component = this.subject();
assert.equal(component._state, 'preRender');

// Renders the component to the page
this.render();
assert.equal(component._state, 'inDOM');
});

我尝试了多种方法将此帮助程序作为依赖项添加到测试中,例如 needs: ['helper:moment'] 并将其初始化程序签名添加到 moduleForComponent 函数的参数中,但没有任何效果我找不到任何关于它的信息。非常感谢您的帮助。

最佳答案

编辑:据此PR ,您应该能够指定 { integration: true } 将您现有的单元测试转换为集成测试,从而消除对 needs: 的需求。

 moduleForComponent('post', { integration: true }); 

可以在这里找到更多背景:https://github.com/rwjblue/ember-qunit/issues/108 .本质上,集成标志会禁用测试的隔离容器,以便它可以访问应用程序的解析器。

注意:需要 ember-qunit@0.2.11。

旧答案:看看这个 PR:https://github.com/switchfly/ember-test-helpers/pull/21它添加了 moduleForIntegration。这是相关的一点:

This adds a new kind of test module that fills a gap in the current set of possible tests. So far we have:

• unit tests that are good for testing algorithmic correctness in isolation.

• acceptance tests that are good for testing within a complete application.

But we're missing the ability to integration test a unit smaller than whole-application. This PR adds a new TestModuleForIntegration that lets you drive an integration test with a template. I think this is often a more appropriate way to test Ember Components than a unit test, because interesting components tend to have rich dependence on other components.

您可以在 liquid-fire tests 中看到它的使用情况:

/* global sinon */
import Ember from "ember";
import moduleForIntegration from "../../helpers/module-for-integration";
import { test } from "ember-qunit";

moduleForIntegration('Integration: liquid-if');

test('it should render', function(assert) {
this.set('person', 'Tom');
this.render(`
{{#liquid-if isReady}}
{{person}} is ready
{{else}}
{{person}} is not ready
{{/liquid-if}}
`); // }}`)

assert.equal(this.$().text().trim(), 'Tom is not ready');
this.set('person', 'Yehuda');
assert.equal(this.$().text().trim(), 'Yehuda is not ready');
this.set('isReady', true);
assert.equal(this.$().text().trim(), 'Yehuda is ready');
});

关于ember.js - 通过 add on 安装 Ember helper 然后在组件中使用它会破坏测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29475291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com