我有依賴於其他服務,從不同的模塊,像這樣一個服務:從不同的模塊中的另一個服務懲戒服務
(function() {
'use strict';
angular
.module('app.core')
.factory('userService', userService);
function authService() {
return: {
userLoggedIn: false
}
}
})();
(function() {
'use strict';
angular
.module('app.services')
.factory('AuthService', authService);
authService.$inject = ['$http', 'userService'];
function authService($http, userService) {
}
我想爲我的authService
寫測試,但我得到的錯誤注射因爲它無法找到userService
beforeEach(function() {
module('app.services');
});
beforeEach(inject(function(_AuthService_) {
authService = _AuthService_;
}));
我怎樣才能解決這個問題,將使用$provide
幫助我在這裏?
UPDATE
我曾嘗試以下,但仍然得到解決
好的錯誤
beforeEach(function() {
module('app.services');
});
beforeEach(inject(function(_AuthService_, _$provide_) {
authService = _AuthService_;
$provide = _$provide_;
}));
beforeEach(function() {
module(function ($provide) {
$provide.value('userService', function(){
return {
userLoggedIn: false
}
});
});
});
,所以我只需要做到以下幾點:
beforeEach(function() {
module('app.dataservices');
module(function ($provide) {
$provide.value('userService', function(){
return {
userLoggedIn: false
}
});
});
});
beforeEach(inject(function(_AuthService_) {
authService = _AuthService_;
}));
測試現在正通過我的
是的,我的問題是,在我的測試中,我只加載了包含AuthService的'app.services'模塊。但是這個服務依賴於包含在不同模塊中的服務。所以我試圖找到一種方法來提供我的'userService'的模擬版本到'AuthService',而不必在我的測試規範中加載'app.core' – mindparse