ember test fails for FormattedDateHelper
On page 121 when I try to run the tests as ember test or npm test I get the following errors:
$ ember test
version: 0.1.4
valid watchman found, version: [3.0.0]
Built project successfully. Stored in "/home/kiffin/projects/emberjs/cli/book-ember-cli-101/tmp/class-tests_dist-Uuo0rogo.tmp".
ok 1 PhantomJS 1.9 - JSHint - adapters: adapters/application.js should pass jshint
...
ok 45 PhantomJS 1.9 - FriendsNewController: it exists
not ok 46 PhantomJS 1.9 - FormattedDateHelper: it works
---
actual: >
null
message: >
Died on test #1 at http://localhost:7357/assets/test-support.js:418
at :13
at http://localhost:7357/assets/vendor.js:77
at http://localhost:7357/assets/test-loader.js:14: 'undefined' is not a function (evaluating 'formattedDate(42)')
Log: >
...
not ok 47 PhantomJS 1.9 - Article: it exists
---
actual: >
null
message: >
Died on test #1 at http://localhost:7357/assets/test-support.js:418
at test (http://localhost:7357/assets/test-support.js:284)
at :17
at http://localhost:7357/assets/vendor.js:77
at http://localhost:7357/assets/test-loader.js:14: No model was found for 'friend'
Log: >
...
ok 48 PhantomJS 1.9 - Friend: it exists
...
ok 55 PhantomJS 1.9 - FriendsShowRoute: it exists
not ok 56 PhantomJS 1.9 - dateHelpers: it works
---
actual: >
null
message: >
Died on test #1 at http://localhost:7357/assets/test-support.js:418
at :13
at http://localhost:7357/assets/vendor.js:77
at http://localhost:7357/assets/test-loader.js:14: 'undefined' is not a function (evaluating 'dateHelpers()')
Log: >
...
ok 57 PhantomJS 1.9 - JSHint - utils: utils/date-helpers.js should pass jshint
1..57
# tests 57
# pass 54
# fail 3
Running ember server starts up just fine.
I suspect it has something to do with the relative paths given in the files tests\unit\helpers\formatted-date-test.js or app/helpers/formatted-date.js
I believe this is because when the generator is used to create the test and the helper, by default it uses the function name formattedDate in the helper and the test.
I don't know if this has any negative effects, but keeping most of the generated code the same and changing out the guts seemed to work for me:
import Ember from 'ember';
import { formatDate } from '../utils/date-helpers';
export function formattedDate(date, format) {
return formatDate(date, format);
}
export default Ember.Handlebars.makeBoundHelper(formattedDate);
Thank you @IanVS, that fixed my test.
Since it's not entirely obvious, I'll phrase it differently here for future visitors: in the section "Writing an Ember helper: formatted-date", the following code is shown:
import Ember from 'ember';
// We are consuming the function defined in our utils/date-helpers.
import { formatDate } from '../utils/date-helpers';
export default Ember.Handlebars.MakeBoundHelper(function(date, format) {
return formatDate(date, format);
});
In this code, the helper is created by feeding an anonymous function to Ember.Handlebars.MakeBoundHelper. However, the test environment can't access this bound helper. Instead, we have to define the function separately, and then create the helper from it, exporting both. This way, the helper can be used and the function that actually implements it can be imported and tested.
There's a tangential, yet related matter: since the introduction of HTMLBars, the usage makeBoundHelper has changed. But that's a different issue, covered in #206.