2016-06-10 158 views
0

我遇到了一些麻煩,單元測試了一個簡單的用戶服務,它暴露了兩個函數。 測試和服務如下。 每當我運行測試,我不斷收到以下錯誤..AngularJS單元測試服務

PhantomJS 2.1.1 (Mac OS X 0.0.0) User Service should contain a UserService FAILED 
    Error: [$injector:unpr] Unknown provider: UserProvider <- User 
    http://errors.angularjs.org/1.5.6/$injector/unpr?p0=UserProvider%20%3C-%20User (line 4501) 

的服務

angular.module('y2yApp').service('User', function($http) { 

    this.profile = function() { 
     return $http({ 
      method: 'GET', 
      url: config.MW_URL + '/profile' 
     }).then(function(response) { 
      return { 
       err: null, 
       data: response.data 
      }; 
     }, function(response) { 
      return { 
       err: response.data, 
       data: null 
      }; 
     }); 
    }; 

    this.update = function(userInfo) { 
     return $http({ 
      method: 'POST', 
      url: config.MW_URL + '/profile', 
      data : userInfo 
     }); 
    }; 
}); 

測試

describe('User Service', function() { 
    beforeEach(function() { 
     angular.module('y2yApp'); 

    }); 
    it('should contain a UserService',inject(['User',function(User) { 
     console.log(User); 
      expect(User).not.to.equal(null); 
    }])); 



}); 

如果一個測試運行,並且我能夠爲了正確地獲得用戶服務實例,我可以繼續編寫其餘的測試,但由於某種原因,它不起作用。有人可以幫我對此並闡明我做錯了什麼在這裏一些輕..

回答

0

should be

module('y2yApp'); 

這是CommonJS的不安全的快捷方式

angular.mock.module('y2yApp'); 
+0

我這樣做,但沒有運氣與測試傳遞.. – Sharath

+0

這是您已發佈的代碼中的錯誤。它未通過的原因可能是Karma配置或任何其他在幕後的配置。 – estus

+0

是的,我的應用程序初始化存在錯誤 – Sharath