0

我知道這裏有很多類似的問題,我經歷了很多,但他們似乎都沒有解決我的問題。AngularJS在'UnknownProvider'測試服務結果

我想測試一個服務,我不斷得到Unknown Provider錯誤,我只是無法弄清楚。

的代碼看起來是這樣的:

TestService.js

'use strict'; 
angular.module('app') 
    .service('TestService', function() { 

     var testFunction = function() { 
      console.log('testFunction'); 
     }; 

     return { 
      testFunction: testFunction 
     }; 
    }); 

test.service.tests.js

'use strict'; 
describe('TestService', function() { 

    beforeEach(module('app')); 

    var TestService; 

    beforeEach(inject(function(_TestService_) { 
     TestService = _TestService_; 
    })); 

    describe('testFunction', function() { 
     it('Should call the test function', function() { 
      expect(TestService.testFunction).toHaveBeenCalled(); 
     }); 
    }); 
}); 

即使我註釋掉expect(testService.testFunction).toHaveBeenCalled();我仍然得到:

Error: [$injector:unpr] Unknown provider: TestServiceProvider <- TestService

我無法弄清楚這一點。我不知道這個代碼是否有問題,或者如果我沒有正確設置測試。

無論如何,任何幫助表示讚賞。

+0

參考此鏈接:https://docs.angularjs.org/error/$injector/unpr – chirag

+0

謝謝,我已經通過該文檔之前,並仔細檢查了所有可能的原因,它概述了錯誤,但他們似乎沒有解決這個錯誤:( –

+0

,但我認爲問題是它無法解決所需的依賴關係 – chirag

回答