我不能 运行 我的项目 Karma 和 Jasmine

I can't run on my project Karma and Jasmine

我照着书AngularJS: Up和运行还有作者说用Karma和Jasmine做测试的那一章,没说太多怎么组织你的项目以及安装 Karma 和 Jasmine 的确切位置。

我安装了nodejs,运行良好。然后我将 xampp/htdocs/angularjs-up-and-running 我的项目和要测试的文件放入。

在我有 none_modules 的同一个文件夹中。在最后一个文件夹中,我有 karma、karma-jasmine 和 karma-chrome-launcher.

我从控制台进入 c:/xampp/htdocs/angularjs-up-and-running/none_modules/karma 文件夹并使用命令:

karma init

我回答了所有问题然后我用了:

karma start

Chrome 是这样打开的:

但我不知道如何测试我的 js 文件。我试图用 http://localhost:9876/controller.js 来测试我的文件,但我在控制台上看到了这个:

Controller.js

angular.module('notesApp', []).controller('ListCtrl', [ function(){
var self = this;
self.items = [
    {id: 1, label: 'First', done: true},
    {id: 2, label: 'Second', done: false}
];

self.getDoneClass = function(item) {
    return {
        finished: item.done,
        unfinished: !item.done
    };
};

}]);

我是 angularjs 和这个测试之王的新手。我在实习生上搜索了解决方案,但我的问题是我不知道如何使用我的文件 controller.js 来测试它,而且我没有找到解决方案。 请有人在这种情况下伸出援手。

有一个名为 karma.conf.js 的文件。在此文件中,您指定一个 'files' 参数和一个文件数组,这些文件包含您想要 运行 的测试。因此,您将编写一个名为 'app/mytest.js' 的测试,并在 karma.conf.js 文件中放置该测试的路径。

module.exports = function(config) {
config.set({
    files: [
        "app/test.js",
    ],   
});
};

请注意,您需要在测试文件本身中包含 angular 模块和控制器依赖项。

所以 app/mytest.js 的内容可能看起来像(来自 angular documentation:

describe('PasswordController', function() {
  beforeEach(module('app'));

  var $controller;

  beforeEach(inject(function(_$controller_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $controller = _$controller_;
  }));

  describe('$scope.grade', function() {
    it('sets the strength to "strong" if the password length is >8 chars', function() {
      var $scope = {};
      var controller = $controller('PasswordController', { $scope: $scope });
      $scope.password = 'longerthaneightchars';
      $scope.grade();
      expect($scope.strength).toEqual('strong');
    });
  });
});