javascript - 将 angular js 服务/工厂注入(inject) Jasmine

标签 javascript angularjs jasmine karma-runner

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

这是我的 app.js

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

这是 services.js

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

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

这是测试

'使用严格';

(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 配置文件中。您需要在文件数组中添加这样一行:

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

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

关于javascript - 将 angular js 服务/工厂注入(inject) Jasmine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23770376/

相关文章:

javascript - rails : javascript/jQuery callbacks are triggered twice

spring Controller 中的 java.lang.StackOverflowError

javascript - 获取嵌套数组中特定键的每个值的总金额

javascript - 如何在 Jasmine 测试中强制首先执行某个函数?

javascript - 如何使用下划线模板 (<%%>) 在 PHP 中使用它 (<?php ?>)

javascript - 页面可见性 API 是否先于脚本中的任何其他内容运行

javascript - 如何在 reactjs 中使用 pug 模板引擎

javascript - AngularJS 对象

javascript - $browser.$$checkUrlChange 在 Jasmine 测试中未定义

Angular2 + karma + protractor - 运行特定的 e2e 测试用例