Search Posts

Category: karma-jasmine

Is it sensible to prevent an angularJS app being bootstrapped when testing with karma-runner?

I have an angularJS 1.6 project written in TypeScript, the entry file contains a statement to boostrap my application: angular.bootstrap(document, [‘app’]); When it comes to unit testing my app (using KarmaJasmine), I currently have a single test for a service belonging to a separate module, that’s a dependency of my app module. I have not yet written any specs that load in my app module I have some log statements in a run block […]

Why am I not getting status code in Jasmine test case?

I am doing a rest call in a following manner updateOperation = function (UUID) { var deferred = $q.defer(), restApiPath = restApiBasePath + UUID + “/_update”, updateSuccess = function () { deferred.resolve(); }, updateFailure = function (errorObj) { deferred.reject(errorObj); }; xyzService.invokeREST(“PUT”, restApiPath, updateSuccess , updateFailure ,true); return deferred.promise; } And my test case looks like this it(“should call update & resolve promise when http response is”, function() { $httpBackend.expectPUT(getValidURL).respond(200, true); taskService.updateOperation(UUID).then(function(status,data){ expect(status).toEqual(200); }); $httpBackend.flush(); }); […]

How to test AngularJS controller using Karma-Jasmine

I am trying to use Jasmine and karma to set up unit tests like explained here, in my already developed Angularjs application. I created a simple test file for my controller like this: describe(‘controller test’, function() { beforeEach(module(‘ipaApp’)); var $controller, $rootScope; beforeEach(inject(function(_$controller_, _$rootScope_) { // The injector unwraps the underscores (_) from around the parameter // names when matching $controller = _$controller_; $rootScope = _$rootScope_; })); describe(‘$scope.grade’, function() { it(‘sets the strength to “strong” if […]

Jasmine Spy : Getting an error in returnValue

I am trying to write unit test case with jasmine spy. Below is my service code. function reset(someParam) { var deferred = $q.defer(); svcTypes.getTasksWithRoles().then(function (types) { if (types.HadError) return; // do some stuff.. deferred.resolve(results); }, function (errorResponse) { deferred.reject(errorResponse); }); return deferred.promise; } Below is my unit test case : describe(‘tests for svcWorkordertypes’, function () { beforeEach(angular.mock.module(‘workorders.service’)); beforeEach(angular.mock.module(‘workordertypes.service’)); var svcWorkordertypes; var svcTypes; var $q, $httpBackend; beforeEach(angular.mock.inject(function (_svcWorkordertypes_, _svcTypes_, _$q_, _$httpBackend_) { svcWorkordertypes = _svcWorkordertypes_; svcTypes […]

Testing JS callback with timeout using Jasmine

I need to test some code in Angular with Jasmine, but the thing is that I can’t do this because of $timeout call. So the code looks like this: $scope.add = function() { SomeService.add(id, function() { $timeout(function() { $scope.showSuccessMessage(); }, 1000) }, function() {}) }; So the test code is: describe(‘method add’, function() { it(‘should add’, function() { spyOn(SomeService, ‘add’).and.callFake(function(id, successCallback, errorCallback) { spyOn(scope, ‘showSuccessMessage’); successCallback(); expect(scope.showSuccessMessage).toHaveBeenCalled(); }); scope.add(); expect(SomeService.add).toHaveBeenCalled(); }); }); And the problem […]

Using Jasmine + Karma to test Angular JS app with ASP.Net MVC

My app is built with MVC and angular.js. There is angular service, myAppConfigService, that is rendered by layout.cshtml page. This is because the myAppConfigService includes some paths which have to be generated by MVC helper method. When I run karma, it show error that the myAppConfigService is not available. I understand this error is because the layout.cshtml is not run by karma. But how can I solve this isseu? Thanks. Chrome 58.0.3029 (Windows 7 […]

Angular unit test with karma jasmine and browserify

I am using this repo code to apply browserify in app. I want to write unit test for my app for this I include karma and karma browserify. My folder structure is : karma.config.js module.exports = function(config) { config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) basePath: ”, // frameworks to use // available frameworks: frameworks: [‘jasmine’ , ‘browserify’], // list of files / patterns to load […]

Jasmine – How to Spy and test an AngularJS asynchronus Service

I am trying to spy on an asynchronous service call, however so far not successful. Though the synchronus calls are able to Mock. commonService.getResource(ResourceConst).then(function (result) { $scope.resource =; }, function (error) { loggerService.log(error); }); Actual Get Resource Function function getResource(resourceURL) { var deferred = $q.defer(); var url = resourceURL + “.json”; $http.get(url).then(function (data) { deferred.resolve(data); }, function (error) { deferred.reject(error); }); return deferred.promise; } Even though I was able to spy on synchronus methods […]

How to unit test angular controller with injected service?

I have a simple controller angular.module(‘testPane’).controller(‘testCtrl’, [‘$scope’,’testService’,function($scope,testService){…}]) I am trying to test this controller beforeEach(function(){ module(‘myModule’); inject(function($injector){ this.$controller = $injector.get(‘$controller’); this.testService= $injector.get(‘testService’); this.$scope = $injector.get(‘$rootScope’).$new(); }) }); function initController(context){ return context.$controller(‘testCtrl’,{ $scope:context.$scope, inputScreenService:context.testService }) } But this keep on throwing Error: [$injector:unpr] Unknown provider: testServiceProvider <- testService$injector/unpr?p0=testServiceProvider%20%3C-%20testService at bower_components/angular/angular.js:68:12 at bower_components/angular/angular.js:4381:19 at Object.getService [as get] (bower_components/angular/angular.js:4529:39) at bower_components/angular/angular.js:4386:45 at Object.getService [as get] (bower_components/angular/angular.js:4529:39) at UserContext.<anonymous> (test/testCtrl-spec.js:41:48) at Object.invoke (bower_components/angular/angular.js:4570:17) at UserContext.workFn (node_modules/angular-mocks/angular-mocks.js:2524:20) at window.inject.angular.mock.inject […]

Next Page »