将角度js服务/工厂注入Jasmine

时间:2014-05-20 21:35:23

标签: javascript angularjs jasmine karma-runner

我正在尝试在我在Angular JS中创建的工厂上运行一些Jasmine Unit测试。

这是我的app.js

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

这是services.js

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

这是Karma conf文件

  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'
    ],

这是测试

'use strict';

(function () {

    describe('myApp', function () {

      beforeEach(module('myApp'));

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

    });
})();

我真的被卡住了,不知道现在走哪条路:( 所有正确的脚本都会加载到浏览器中。此外,该应用程序在浏览器中以正常模式工作,因此服务工厂很好。

错误消息说:

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.

2 个答案:

答案 0 :(得分:2)

问题正是消息所说的:ngRoute不可用,因为你没有在你的Karma配置文件中包含该脚本。您需要在文件数组中添加这样的一行:

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

我不知道路径是什么,所以你可能需要调整它。

答案 1 :(得分:0)

只是为了帮助一些人,查看设置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'
    ],
相关问题