dragon dragon - 22 days ago 8
AngularJS Question

I can't run on my project Karma and Jasmine

I follow the book AngularJS: Up and Running and at the chapter where the author said to use Karma and Jasmine for testing, he didn't said too many words how to organize your project and where exactly to install Karma and Jasmine.

I installed nodejs and runs well. Then I put in xampp/htdocs/angularjs-up-and-running my project with the file to test.

In the same folder I have none_modules. In this last folder I have karma, karma-jasmine and karma-chrome-launcher.

From the console I go in the c:/xampp/htdocs/angularjs-up-and-running/none_modules/karma folder and I used the command:

karma init


I answered to all questions then I used then:

karma start


Chrome is opening like this:
Chrome Debug

but I don't know how to test my js file. I tried to put http://localhost:9876/controller.js to test my file but I got this on my Console:

Console Image

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
};
};


}]);

I'm new to angularjs and this king of testing. I searched on the interned for solutions but my problem is that I don't know how to use my file controller.js to test it, and I didn't find a solution.
Please someone give a hand in this situation.

Answer

There's a file called karma.conf.js. In this file, you specify a 'files' parameter with an array of files that contain the tests that you want to run. So you would write a test called 'app/mytest.js' and in your karma.conf.js file, you would put the path to that test.

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

Note that you need to include your angular module and controller dependencies in the test file itself.

So contents of app/mytest.js might look like (from the 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');
    });
  });
});