簡體   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