Search Posts

Category: unit-testing

Jasmine testing with $httpBackend for Angularjs controller

I made an Angular project — a simple music library application. It allows you to display, edit or delete data about albums. Communication with a server is made via API. For now, the project works pretty good, but I need to write unit tests. The problem is, I can’t find a clear tutorial on how to do it in my case. Let me provide you my code. albums.controller.js: ‘use strict’; angular.module(‘albums’).controller(‘AlbumsController’, [‘AlbumService’, ‘$location’, function (AlbumService, […]

Why does a failed assertion trigger a promise catch?

While writing unit tests for an angular app I was experiencing unexpected results. I was able to condense the unexpected behavior into a sample test. The should.equal(true, false, ‘should then’) assertion failure in the then block seems to trigger the catch block of the promise. describe.only(‘test’, function () { var $q, $rootScope; beforeEach(function () { inject(function(_$q_, _$rootScope_) { $q = _$q_; $rootScope = _$rootScope_.$new(); }); }); var stubService = sinon.stub(service, ‘getPanel’); it(‘shall…’, function() { //1 […]

Jasmine spyOn does not mock function correctly

So I have a function like so: function initializeView() { var deferred = $q.defer(); var deleteOrUploadToBestMoments = this.deleteOrUploadToBestMoments; var checkAndDeleteExpiredMoments = this.checkAndDeleteExpiredMoments; getNearbyMoments() .then(deleteOrUploadToBestMoments) .then(checkAndDeleteExpiredMoments).then(function(moments) { //omitted }, function(error) { deferred.reject(error); }); return deferred.promise; }; I wouldl ike to test this function with a unit test. Here is my test: it(‘Should call initializeView’, function(done) { spyOn(service, ‘getNearbyMoments’).and.callFake(function() { console.log(“MOCKED getNearbyMoments”); return $q.resolve(mockOutMoments()); }); spyOn(service, ‘deleteOrUploadToBestMoments’).and.callFake(function() { console.log(“MOCKED deleteOrUploadToBestMoments”); return $q.resolve(); }); spyOn(service, ‘checkAndDeleteExpiredMoments’).and.callFake(function() { console.log(“MOCKED […]

jasmine test does not move into the then part of the promise

I would like to test this function: function initializeView() { var deferred = $q.defer(); if(this.momentArray) { core.listMoments(constants.BEST_MOMENT_PREFIX, ”).then(function(moments) { //Ommitted deferred.resolve(moments); }, function(error) { console.log(“ERROR”); deferred.reject(error); }); } else { deferred.resolve(); } return deferred.promise; }; The function calls core.listMoments: function listMoments(prefix, startAfter) { // var deferred = $q.defer(); var promises = []; return awsServices.getMoments(prefix, startAfter).then(function(moments) { //Mocked console.log(“getMoments Returned”); //Does not print for(var i = 0; i < moments.length; i++) { // moments[i].Key = constants.IMAGE_URL […]