Encountering an error while running Jasmine tests when trying to mock/spy on an API call in the tests. Below is the code snippet responsible for calling the API:
UploadedReleasesController.$inject = ['$log', '$scope', '$filter', '$modal', 'ReleaseService', 'TrackService', 'APP_CONFIG', 'DeliveriesService'];
function UploadedReleasesController ($log, $scope, $filter, $modal, releaseService, trackService, APP_CONFIG, deliveriesService){
.
.
.
releaseService.releases(vm.currentWorkspace).then(function (responseValues) {
vm.albums = responseValues;
if(vm.albums.length !== 0){
vm.selected.album = vm.albums[0];
In this code, releaseService
represents a defined service and releases
is one of its methods that needs to be spied on. It requires a string argument such as 'HIGH'
. The test file attempts to mock this call:
describe('app module', function() {
var vm, scope, modalInstance, releaseService, trackService, deliveriesService;
beforeEach(module('app.uploadedReleases')); // Main module name
beforeEach(module('app.config'));
beforeEach(module('auth'));
beforeEach(function() {
var mockReleasesData = {
"test" : 100
}
};
releaseService = jasmine.createSpyObj("releaseService", ["releases"]);
releaseService.releases('HIGH').and.returnValue(mockReleasesData);
});
beforeEach(inject(function($controller, $log, $rootScope, $filter, APP_CONFIG) {
scope = $rootScope.$new();
modalInstance = {
close: jasmine.createSpy('modalInstance.close'),
open: jasmine.createSpy('modalInstance.open'),
dismiss: jasmine.createSpy('modalInstance.dismiss'),
result: {
then: jasmine.createSpy('modalInstance.result.then')
}
};
vm = $controller('UploadedReleasesController', {'APP_CONFIG':APP_CONFIG, '$log':$log, '$scope':scope, '$filter':$filter, '$modal':modalInstance,
'ReleaseService':releaseService, 'TrackService':trackService, 'DeliveriesService':deliveriesService});
}));
However, upon executing the test, the following error occurs:
TypeError: 'undefined' is not an object (evaluating 'releaseService.releases('HIGH').and') at test-release.controller.spec.js:93 TypeError: 'undefined' is not an object (evaluating 'releaseService.releases(vm.currentWorkspace).then') undefined
The actual implementation of releaseService can be found in release.service.js
releaseService.releases = getReleases;
.
.
.
function getReleases(workspace){
var releases = [];
headers.Workspace = workspace; // set the workspace
var deferred = $q.defer();
// Perform $http calls and return a promise
.
.