繁体   English   中英

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

[英]Injecting angular js service/factory into Jasmine

我试图在我在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'
    ],

这是测试

'使用严格';

(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.

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM