2

ジャスミンでサービスをテストしようとしていますが、「不明なプロバイダー: AuthServiceProvider <- 2683 行目の angular/angular.js の AuthService」というメッセージが表示され続けます。

私のサービスが定義されています:

app.factory( 'AuthService', ["$resource", "$rootScope", "apiPrefix", function($resource, $rootScope,  apiPrefix) {
  auth_resource = $resource(apiPrefix + "/session", {}, {
    logout: {method:'GET'}
  });

  var currentUser;
  return {
    login: function(email, password, success, failure) {
      auth_resource.save({}, {
        email: email,
        password: password
      }, function(response){
        currentUser = response
        success()
      }, function(response){
        failure()
       });
    },
    logout: function(success, failure) { 
      auth_resource.logout( 
        function(response){ 
          currentUser = undefined 
        }, function(){
          $scope.alerts.push({type: "success", msg: "Logged out" }) 
        }, function(){
          $scope.alerts.push({type: "error", msg: "Sorry, something went wrong" })           
        }
      )
     },
    isLoggedIn: function(){ return currentUser !== undefined},
    currentUser: function() { return currentUser; }
   };
}]);

そして私のテスト:

describe("AuthService", function(){
  var httpBackend;
  beforeEach(inject(function($httpBackend, AuthService){
    module('app');

    httpBackend = $httpBackend;
    AService = AuthService;
  }));


  it("should login the user", function(){
    // test here
  });
});

私のジャスミン設定ファイルは次のとおりです。

// This pulls in all your specs from the javascripts directory into Jasmine:
// spec/javascripts/*_spec.js.coffee
//  spec/javascripts/*_spec.js
// spec/javascripts/*_spec.js.erb

//= require application
//= require_tree ./

コントローラーを正常にテストできるため、これは適切に構成されているようですが、サービスが認識されない理由がわかりません。

4

1 に答える 1

1

を使用$injectorしてサービスを取得し、次のように実際のテストに挿入できます

describe("AuthService", function () {
    var httpBackend, AService, apiPrefix;
    beforeEach(module('app'));

    beforeEach(function () {
        angular.mock.inject(function ($injector) {
            httpBackend = $injector.get('$httpBackend');

            apiPrefix = angular.mock.module('apiPrefix'); // I assume you have apiPrefix module defined somewhere in your code.
            AService = $injector.get('AuthService', {apiPrefix: apiPrefix});
        })
    });

    it("should login the user", inject(function (AService) {
        // test here
    }));
});

コードのどこかに apiPrefix モジュールが定義されていると仮定します。

于 2013-08-27T04:19:34.430 に答える