describe('Controller:insightSettingsController', function() {
'use strict';
var insightSettingsCtrl;
var settingsService;
var UtilsService;
var scope;
var updateMethodDeferred;
var httpBackend;
var deferred;
var q;
var spy;
beforeEach(module('ui.router',proname.components.insightSettings.module.name));
beforeEach(function() {
var mockUtilsService = {};
module(function(_$provide_) {
_$provide_.value('UtilsService', mockUtilsService);
});
inject(function($q) {
deferred = $q.defer();
mockUtilsService.getConfigurations = function() {};
spyOn(mockUtilsService, 'getConfigurations')
.and.returnValue(deferred.promise);
});
});
beforeEach(inject(function(
_$controller_, _$httpBackend_, _$rootScope_, _settingsService_,
_UtilsService_, _$q_) {
scope = _$rootScope_.$new();
spy = jasmine.createSpy();
settingsService = _settingsService_;
UtilsService = _UtilsService_;
httpBackend = _$httpBackend_;
insightSettingsCtrl = function() {
return _$controller_(
dbmcm.components.settings.insightSettingsController,
{$scope: scope, UtilsService: UtilsService});
};
}));
describe(
'Check Existing Function , parameters, Initialization of function',
function() {
it('should call UtilsService.getConfigurations() once',
function() {
insightSettingsCtrl();
deferred.resolve();
scope.$digest();
expect(UtilsService.getConfigurations).toHaveBeenCalled();
});
});
});
喜的特性「service.method」,我寫的角度測試用例我單獨module.Unfortunately,我模塊上方擊穿我的測試情況下,收到錯誤debug.js: 44未捕獲類型錯誤:無法讀取未定義的屬性'獲取配置'。我在控制器的初始化時發射getConfigurations功能請低於遺漏的類型錯誤:無法讀取未定義
constructor: function(UtilsService) {
UtilsService.getConfigurations().then(function(response) {
this.utilsConfig_ = response;
}.bind(this));
}
一看,我認爲茉莉花的間諜,只檢查是否函數被調用,不費一槍實際執行。這就是爲什麼getConfigurations()然後拋出一個錯誤。 爲了這個,我在我的測試情況下適用.and.callThrough()
it('should call UtilsService.getConfigurations() once',
function() {
spyOn(UtilsService, 'getConfigurations').and.callThrough();
insightSettingsCtrl();
deferred.resolve();
scope.$digest();
expect(UtilsService.getConfigurations).toHaveBeenCalled();
});
});
在那之後我得到debug.js:44未捕獲的錯誤:getConfigurations已經在
窺探請指正我做錯了。 在此先感謝
你能告訴我們這是debug.js LN 44? –