Я написал примеры модульных тестов для наблюдаемой службы и ее сбоя при попытке проверить действительные функции службы.
Я получаю ожидаемого шпиона, но получаю неопределенное сообщение об ошибке.
mySvcApp.js:
var mySvcApp = angular.module('mySvcApp', []);
mySvcApp.service('observerService', [function() {
var arrOfObsv = [];
return {
callMasterRecipeDialog: callMasterRecipeDialog,
registerObserver: registerObserver,
};
function callMasterRecipeDialog(callback) {
arrOfObsv.push(callback);
}
function registerObserver(tabData) {
arrOfObsv.forEach(function(arrOv) {
arrOv(tabData);
});
}
}]);
myObserverSpec.js:
describe('testing observerService', function(){
var observerService;
var arrOfObsv =[];
beforeEach(function(){
module('mySvcApp');
// do injection in the argument of this 'function'
inject(function($injector){
observerService = $injector.get('observerService');
});
});
describe('callMasterRecipeDialog', function() {
it('1. Should check if callMasterRecipeDialog method of Observer Service is defined', function() {
expect(observerService.callMasterRecipeDialog).toBeDefined();
});
it('2. Should execute callMasterRecipeDialog method of Observer Service without any errors', function() {
var callback = function() {};
observerService.callMasterRecipeDialog(callback);
expect(observerService.callMasterRecipeDialog).toHaveBeenCalled();
});
it('3. Should check if the length of an array is greater than zero or not', function() {
var callback = function() {};
arrOfObsv.push(callback);
expect(arrOfObsv.length).toBeGreaterThan(0);
});
});
describe('registerObserver', function() {
it('1. Should check if registerObserver method of Observer Service is defined', function() {
expect(observerService.registerObserver).toBeDefined();
});
it('2. Should execute registerObserver method of Observer Service without any errors', function() {
var tabData = {
name: 'Order1'
}
spyOn(observerService, 'registerObserver');
observerService.registerObserver(tabData);
expect(observerService.registerObserver).toHaveBeenCalled();
});
});
});
См. Ссылку на плункер здесь
canкто-нибудь подскажет, как проверить реальные методы внутри сервиса и как исправить ошибку.