Javascript Jasmine测试失败,需要API

Javascript Jasmine测试失败,需要API,javascript,unit-testing,ember.js,jasmine,Javascript,Unit Testing,Ember.js,Jasmine,我正在和Jasmine一起进行一个EmberJS项目的单元测试,但是我在Ember的需求API方面遇到了麻烦 当我尝试运行jasmine测试时,如果控制器有“需要”以及调用 这个._super() 我得到这个控制台错误 “无法调用null的方法'has'” 当我试着调试的时候,一路把我带到了灰烬的深处,但我什么也没得到 有人知道我做错了什么吗 Application.SearchPendingController = Ember.ObjectController.extend({ nee

我正在和Jasmine一起进行一个EmberJS项目的单元测试,但是我在Ember的需求API方面遇到了麻烦

当我尝试运行jasmine测试时,如果控制器有“需要”以及调用

这个._super()

我得到这个控制台错误

“无法调用null的方法'has'”

当我试着调试的时候,一路把我带到了灰烬的深处,但我什么也没得到

有人知道我做错了什么吗

Application.SearchPendingController = Ember.ObjectController.extend({
    needs: ['searchResults', 'search'],
    shouldDisable: false,
    searchResultsController: null,
    init: function () {
        this._super();

        this.set('searchResultsController', this.controllerFor('searchResults'));

        this.get('controllers.search.content').reload();

        this.get('controllers.searchResults').set('content', this.get('controllers.search.content.results'));

    },
    transitionToResults: function () {
        console.log('yay');
    }.observes('this.searchResultsController.content')
});
当我尝试创建此控制器时,jasmine测试抛出一个错误

var searchPendingController = Application.SearchPendingController.create();

有人对此有什么想法吗?

当您创建控制器时,Ember.js会检查init方法中的依赖项(
needs
)。检查依赖项假设您有一个Ember.js应用程序,并且该应用程序的容器位于控制器的
container
属性中。如果Ember.js为您创建了控制器,那么这一切都非常有效

您的错误正在
verifyDependencies
函数中发生

如果您不希望Ember.js为您创建控制器并希望手动创建它(这就是您在这里所做的),则需要手动将控制器的
容器
属性设置为应用程序的容器

Application.SearchPendingController.reopen({
  container: Application.__container__
});

单元测试控制器很棘手,需要深入了解Ember.js的内部结构。我的建议是,让Ember.js为您创建控制器,并使用集成测试而不是单元测试它们。

创建控制器时,Ember.js会检查init方法中的依赖项(
需要
)。检查依赖项假设您有一个Ember.js应用程序,并且该应用程序的容器位于控制器的
container
属性中。如果Ember.js为您创建了控制器,那么这一切都非常有效

您的错误正在
verifyDependencies
函数中发生

如果您不希望Ember.js为您创建控制器并希望手动创建它(这就是您在这里所做的),则需要手动将控制器的
容器
属性设置为应用程序的容器

Application.SearchPendingController.reopen({
  container: Application.__container__
});

单元测试控制器很棘手,需要深入了解Ember.js的内部结构。我的建议是,让Ember.js为您创建控制器,并使用集成测试而不是单元测试它们。

如果您的控制器需要访问其他控制器以计算某些属性,则更好的方法是让容器为您创建控制器

Application.SearchPendingController = Ember.ObjectController.extend({
  needs: ['searchResults', 'search'],
测试

  var searchModel = something, 
    searchResultsModel = something, 
    searchPendingModel = something;

var container = Application.__container__,
  searchController = container.lookup('controller:search'),
  searchResultsController = container.lookup('controller:searchResults'),
  searchPendingController = container.lookup('controller:searchPending'),
  searchController.set('model', searchModel),
  searchResultsController.set('model', searchResultsModel ),
  searchPendingController.set('model', searchPendingModel );

更好的是,如果您的控制器需要访问另一个控制器来计算某些属性,则让容器为您创建控制器

Application.SearchPendingController = Ember.ObjectController.extend({
  needs: ['searchResults', 'search'],
测试

  var searchModel = something, 
    searchResultsModel = something, 
    searchPendingModel = something;

var container = Application.__container__,
  searchController = container.lookup('controller:search'),
  searchResultsController = container.lookup('controller:searchResults'),
  searchPendingController = container.lookup('controller:searchPending'),
  searchController.set('model', searchModel),
  searchResultsController.set('model', searchResultsModel ),
  searchPendingController.set('model', searchPendingModel );

这对我来说非常有效(减去分号:p)。非常感谢,谢谢你。似乎是一张愚蠢的支票。。这不应该是默认行为吗?我有一个视图,它在
didInsertElement
上调用
this.get('controller').domload()
,该视图实例化了相关的控制器。。。我还认为我可能可以做
this.set('childController',App.childController.createWithMixins({container:this})
,但这不起作用…但同样,使用
App.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu容器
工作得很好对我来说。非常感谢。谢谢。这看起来像是一个愚蠢的检查。这难道不是默认行为吗?我有一个视图,它在
didInsertElement
上调用
this.get('controller').domload()
,它实例化了有问题的控制器……我还认为我可能能够执行
this.set('childController',App.childController.createWithMixins({container:this})
但这不起作用…但同样,使用
App.\uuuuuu container\uuuu
起作用了。。。