Search Posts

Category: jasmine

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, […]

controller is undefined in unit test – jasmin karma

I am writing test for directive. ‘controller must be created’ is failed saying ‘Expected undefined to be defined.’ I am trying to retrieve the controller with element.controller but somehow it is not working. please suggest if I am doing something wrong here. thanks I already tried to find the solution with similar questions but that did not work. import { MyGridCtrl } from ‘../Mygrid.ctrl’; describe(‘My grid component tests’, () => { var _scope: angular.IScope; var […]

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 […]

trying to write test cases for each and every line

have written test case for jumping method, but its not going inside onloadend method seat.onloadend, when I see code coverage report. in createSpyObj i called loadend but still its not going inside can you guys tell me how to fix it. providing my code and test case below. I am trying to wite test case for each and every line. jumping(inputValue: any): void { var that = this; var file: File = inputValue.files[0]; var seat: […]

How to add a TypeScript class into a spec.js file

I am using Jasmine to test an angularjs web app. In one of my spec js file, I want to initiate this TypeScript class, so I can create an object in my unit test. I know how to add service as a dependency into jasmine test file, but I do not know how to add a TypeScript class as a dependency. Please help. This is my TypeScript class: namespace app.api{ ‘use strict’; export class FooClass […]