使用toBeDefined的karma测试控制器失败

时间:2017-01-02 18:11:39

标签: javascript angularjs jasmine karma-runner

我的测试失败了,因为它说我的控制器没有定义。很奇怪,我认为我做的一切都很正确。

describe('homeCtrl', function() {
  var httpBackend, controller, scope;

  beforeEach(module('App'));

  beforeEach(inject(function($httpBackend, $controller) {
    scope = {};
    httpBackend = $httpBackend;
    controller = $controller('homeCtrl', { $scope: scope });
  }));

  it('should exist', function() {
    expect(controller).toBeDefined();
  });
});

我有我的home.js,这是像这样的控制器

var App = angular.module('App')

App.controller('homeCtrl', function($scope) {

})

错误为Expected undefined to be defined.

1 个答案:

答案 0 :(得分:1)

您的 home.js 应该在模块中注入依赖项,将其更改为

var App = angular.module('App',[])
App.controller('homeCtrl', function($scope) {

})