无法从插件项目单元测试一个帮手 [英] Having trouble unit testing a helper from an addon project
问题描述
https://github.com/stefanpenner/ember-cli/issues/2421
ember-cli:1.2
ember-cli: 1.2
我有一个样板 addon 项目有一个title-case帮助器如下:
I have a boilerplate addon project that has a title-case helper as follows:
我的助手 app / helpers / title-case.js
import Ember from 'ember';
export default Ember.Handlebars.makeBoundHelper(function(string) {
if (typeof string === 'string') {
//replace dashes with spaces
var str = string.dasherize().replace(/-/g, ' ');
return str.replace(/\w\S*/g, function(word){
return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
});
} else {
return string;
}
});
我使用ember-cli生成帮助者的测试
I Generated the test for the helper using ember-cli
ember g helper-test title-case
这是输出:
import {
titleCase
} from 'boilerplate/helpers/title-case';
module('TitleCaseHelper');
// Replace this with your real tests.
test('it works', function() {
var result = titleCase(42);
ok(result);
});
现在从ember-cli运行测试
Now running tests from ember-cli
ember test
获取以下错误:
Build failed.
File: dummy/tests/unit/helpers/title-case-test.js
ENOENT, no such file or directory '/home/me/git/ember/boilerplate/tmp/tree_merger-tmp_dest_dir-PL6HFkuw.tmp/boilerplate/helpers/title-case.js'
Error: ENOENT, no such file or directory '/home/me/git/ember/boilerplate/tmp/tree_merger-tmp_dest_dir-PL6HFkuw.tmp/boilerplate/helpers/title-case.js'
更新
我尝试将以下内容更改为dummy而不是自动生成的样板,并删除了大括号。
UPDATE
I tried changing the following to "dummy" instead of the autogenerated "boilerplate" and removed the curly brackets.
//import {
// titleCase
//} from 'dummy/helpers/title-case';
import titleCase from 'dummy/helpers/title-case';
它现在进入测试方法,但在调用 titleCase 42)
with:
it gets further now and into the test method but failed when calling titleCase(42)
with:
TypeError: Cannot read property 'isUnbound' of undefined
更新#2
我能够得到这个工作,这是丑的,我需要访问 ._ rawFunction
属性并更改import语句的格式。
UPDATE #2
I was able to get this working but it is ugly, I needed to access the ._rawFunction
property and change the format of the import statement.
import titleCaseHelper from 'dummy/helpers/title-case';
var titleCase = titleCaseHelper._rawFunction;
module('TitleCaseHelper');
test('Title case lower case letters', function() {
var result = titleCase('hello world');
equal(result, 'Hello World');
});
我仍然感到困惑的是为什么ember-cli原始生成的测试没有起作用。 / p>
I am still confused as to why the original generated test by ember-cli didn't work.
推荐答案
helper-test generator希望帮助文件具有特殊格式,您可以看到如果您生成帮助器: / p>
The helper-test generator expects the helper file to have a particlular format, which you can see if you generate the helper:
ember generate helper hello
这将生成文件app / helpers / hello.js,其中包含
That will generate the file app/helpers/hello.js, which contains
import Ember from 'ember';
export function hello(input) {
return input;
};
export default Ember.Handlebars.makeBoundHelper(hello);
它还将在tests / unit / helpers / hello-test中生成上述单元测试。从ember-empty / helpers / hello的
It will also generate a unit test for the above in tests/unit/helpers/hello-test.js with
import {
hello
} from 'ember-empty/helpers/hello';
module('HelloHelper');
// Replace this with your real tests.
test('it works', function() {
var result = hello(42);
ok(result);
});
换句话说,辅助测试生成器希望您还可以导出原始函数本身,另外到绑定的帮手。
In other words, the helper-test generator expects that you also export the raw function itself, in addition to the bound helper.
这篇关于无法从插件项目单元测试一个帮手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!