Having trouble unit testing a helper from an addon project

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 20:53:28

问题


https://github.com/stefanpenner/ember-cli/issues/2421

ember-cli: 1.2

I have a boilerplate addon project that has a title-case helper as follows:

My Helper 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;
    }

});

I Generated the test for the helper using ember-cli

ember g helper-test title-case

This was the output:

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);
});

Now running tests from ember-cli

ember test

Getting the following error:

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'

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';

it gets further now and into the test method but failed when calling titleCase(42) with:

TypeError: Cannot read property 'isUnbound' of undefined

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');
});

I am still confused as to why the original generated test by ember-cli didn't work.


回答1:


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

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);

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.



来源:https://stackoverflow.com/questions/26687039/having-trouble-unit-testing-a-helper-from-an-addon-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!