常规控制器工作但测试失败

时间:2014-11-27 10:30:49

标签: ember.js ember-data

我正在使用ember-cli构建Ember应用程序,并且出于某种原因,在运行时工作的非常普通的Controller在运行测试时失败,并出现以下错误:

 TypeError: Cannot read property 'find' of null

控制器只不过是:

export default Ember.ArrayController.extend({
    exerciseTemplates: null,
    setup: function() {
        this.set('exerciseTemplates', this.get('store').find('exercise-template'));
    }.on('init'),

});

有什么想法吗?


BTW,这是堆栈跟踪:

TypeError: Cannot read property 'find' of null
    at eval [as setup] (trainer/controllers/workouts.js:11:54)
    at sendEvent (http://localhost:4200/assets/vendor.js:27786:27)
    at new Class (http://localhost:4200/assets/vendor.js:47448:9)
    at Function.Mixin.create.create (http://localhost:4200/assets/vendor.js:47903:16)
    at Object.defaultSubject [as subject] (http://localhost:4200/assets/test-support.js:200:22)
    at Object.context.(anonymous function) [as subject] (http://localhost:4200/assets/test-support.js:217:38)
    at Object.eval (trainer/tests/unit/controllers/workouts-test.js:15:29)
    at Object.wrapper (http://localhost:4200/assets/test-support.js:272:31)
    at Object.Test.run (http://localhost:4200/assets/test-support.js:1084:18)
    at http://localhost:4200/assets/test-support.js:11

1 个答案:

答案 0 :(得分:-1)

store作为this.store注入到控制器中 - 如果您将代码更改为使用this.store而不是this.get('store'),它是否有效?


更新 - 尝试提供更有用的答案:

查看ember-data tests的内容,我看到有DS._setupContainer方法。从@ givanse上面的评论我认为在测试设置中添加以下内容可能有所帮助:

var container = new Ember.Container();
DS._setupContainer(container);