Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Can';t在karma单元测试中实例化角度控制器_Angularjs_Unit Testing_Gulp_Karma Jasmine - Fatal编程技术网

Angularjs Can';t在karma单元测试中实例化角度控制器

Angularjs Can';t在karma单元测试中实例化角度控制器,angularjs,unit-testing,gulp,karma-jasmine,Angularjs,Unit Testing,Gulp,Karma Jasmine,我在一个生成的项目(由yoeman编写)中遇到了一些问题,它正在测试中 我想要的是构建一个完全自动化的测试环境。我用大口大口、因果报应、茉莉花和有棱角的嘲弄 TypeError: undefined is not an object (evaluating 'controller.awesomeThings') 在“gulp test”之后的我的控制台中,它会抛出此错误消息。但这对我来说毫无意义 看看我的测试规范: 'use strict'; describe('Controller: Ab

我在一个生成的项目(由yoeman编写)中遇到了一些问题,它正在测试中

我想要的是构建一个完全自动化的测试环境。我用大口大口、因果报应、茉莉花和有棱角的嘲弄

TypeError: undefined is not an object (evaluating 'controller.awesomeThings')
在“gulp test”之后的我的控制台中,它会抛出此错误消息。但这对我来说毫无意义

看看我的测试规范:

'use strict';

describe('Controller: AboutCtrl', function() {

  // load the controller's module
  beforeEach(function() {
    module('evoriApp');
  });

  var controller;
  var scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function($controller, $rootScope) {
    scope = $rootScope.$new();
    controller = $controller('AboutCtrl', {
      '$scope': scope
    });
  }));

  it('should attach a list of awesomeThings to the scope', function() {
    console.log(controller);
    expect(controller.awesomeThings.length).toBe(3);
  });
});
karma应该有它需要的任何文件(karma.conf.js):

这是生成的gulp方法的外观:

gulp.task('test', ['start:server:test'], function () {
  var testToFiles = paths.testRequire.concat(paths.scripts, paths.mocks, paths.test);
  return gulp.src(testToFiles)
    .pipe($.karma({
      configFile: paths.karma,
      action: 'watch'
    }));
});
我不知道失败在哪里。我检查了所有路径并多次重写测试文件。。。但是错误并没有改变

有人知道错误是由什么引起的吗

好了,伙计们,我明白了

我试图在jasmine独立环境中运行测试,以最小化错误根所在的区域

这就是我的控制器的外观:

angular.module('evoriApp')
  .controller('AboutCtrl', function ($scope) {
    $scope.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
    $scope.testVariable = 2;
  });
我将'this.awesomeThings'重写为'$scope.awesomeThings',但没有理解,这是不同的东西。不管怎样

这是目前正在运行的规范:

describe('Controller: AboutCtrl', function() {

  // load the controller's module
  beforeEach(function() {
    module('evoriApp');
    console.log("1");
  });

  var controller;
  var scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function($controller, $rootScope) {
    scope = $rootScope.$new();
    controller = $controller('AboutCtrl', {
      $scope: scope
    });
  }));

  it('sollte instanziert worden sein', function() {
    expect(controller).toBeDefined();
  });

  it('sollte ein Object "awesomeThings" besitzen', function() {
    expect(scope.awesomeThings).toBeDefined();
  });

  it('should attach a list of awesomeThings to the scope', function() {
    expect(scope.awesomeThings.length).toBe(3);
  });
});
我学到的是:在测试中,您必须自己生成作用域,并将其传递给控制器,之后您必须将传递的作用域变量用于$scope.variables

describe('Controller: AboutCtrl', function() {

  // load the controller's module
  beforeEach(function() {
    module('evoriApp');
    console.log("1");
  });

  var controller;
  var scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function($controller, $rootScope) {
    scope = $rootScope.$new();
    controller = $controller('AboutCtrl', {
      $scope: scope
    });
  }));

  it('sollte instanziert worden sein', function() {
    expect(controller).toBeDefined();
  });

  it('sollte ein Object "awesomeThings" besitzen', function() {
    expect(scope.awesomeThings).toBeDefined();
  });

  it('should attach a list of awesomeThings to the scope', function() {
    expect(scope.awesomeThings.length).toBe(3);
  });
});