Implement angular js service / factory in jasmine

I am trying to run the Jasmine Unit test on a factory that I created in Angular JS.

Here is my app.js

var app = angular.module('myApp', [
    'ngRoute'
]);

Here is services.js

app.factory('Service', function(){
    var service = {
        one: function(){
            return 1;
          }
      };
    return service;
  })

Here is the Karma config file

  files: [
      'public/bower_components/angular/angular.js',
      'public/bower_components/angular-mocks/angular-mocks.js',
      'public/js/app.js',
      'public/js/controllers.js',
      'public/js/services.js',
      'test/**/*test.js'
    ],

And here is the test

'use strict';

(function () {

    describe('myApp', function () {

      beforeEach(module('myApp'));

      it('testing the service', inject(function (Service) {
        console.log(Service)
      }));

    });
})();

I am really stuck and don’t know where to go now :( All the correct scripts are loaded into the browser. Also, the application works as usual in the browser, so the factory service is fine.

The error message says:

Error: [$injector:modulerr] Failed to instantiate module myApp due to:
    Error: [$injector:modulerr] Failed to instantiate module ngRoute due to:
    Error: [$injector:nomod] Module 'ngRoute' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
+4
source share
2 answers

The problem is what the message says: ngRoute is unavailable because you did not include this script in your Karma configuration file. You need to add this line to the file array:

'public/bower_components/angular-route/angular-route.js',

, , .

+2

, , Karma- , index.html.

index.html , .

, Karma, , .

, , !

 files: [
      'public/bower_components/angular/angular.js',
      'public/bower_components/angular-mocks/angular-mocks.js',
      'public/js/app.js',
      'public/js/controllers.js',
      'public/js/services.js',
      'test/**/*test.js'
    ],
0

Source: https://habr.com/ru/post/1541346/


All Articles