AngularJS $httpBackend expectGET not working

Issue I am writing QUnit tests for an AngularJS factory. Here’s the code for the factory: var app = angular.module(‘App’, []); app.factory(‘$groupFactory’, function($rootScope, $http) { return { ‘getAll’: function(_callback) { $http.get(“get/values/from/server”, { headers: { ‘Content-type’: ‘application/json’ } }).success(function(data, status, headers,

Continue reading

How do I mock $window injected manually in provider private function?

Issue I have the following provider: angular.module(‘MyApp’).provider(‘MyDevice’, function () { var ngInjector = angular.injector([‘ng’]), $window = ngInjector.get(‘$window’); function isMobileDevice () { return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/) .test($window.navigator.userAgent || $window.navigator.vendor || $window.opera); } this.$get = function () { return { isDesktop: function ()

Continue reading

karma/jasmine/angular toHaveBeenCalledWith is really called

Issue in my app for instance I’ve got $rootScope.$emit(‘loggedin’,data.user); $rootScope.$on(‘loggedin’, function(event,user) { console.log(‘called’); }); in my test spyOn($rootScope, ‘$emit’); var response = { “success”:1, “user”:{ “id”:1, “email”:”lama@test.test”, “fullname”:”Lama user”, “username”:”lamauser”, “groups”:[“Users”] }, “logged”:1 }; $httpBackend.when(‘POST’, ‘/api/v1/user’).respond(200,response); $scope.save(); $httpBackend.flush(); expect($scope.errors.length).toEqual(0); expect($rootScope.$emit).toHaveBeenCalledWith(‘loggedin’,response.user);

Continue reading

karma/angularjs how to test run block with an asynchronous service

Issue How can I test like: init.js lama.system module angular.module(‘lama.system’, []) .config([‘$httpProvider’, function($httpProvider) { // Crossdomain requests not allowed if you want do cors request see filter.php $httpProvider.defaults.headers.common[‘X-Requested-With’] = ‘XMLHttpRequest’; }]) .run([‘$rootScope’, ‘$state’, ‘$log’, ‘Global’,function ($rootScope, $state, $log, Global) {

Continue reading

AngularJS – interval doesn't get flushed

Issue I have the angular service function that I am willing to test: doSomeTask: function (ids, callback) { $http.put(‘/get_job’, {‘ids’: ids}).success(function (data) { var statusCheck = $interval(function() { $http.get(‘check_status?=’ + data.job_id).success(function (data) { if(data.completed) { $interval.cancel(statusCheck); // and do something…

Continue reading

How to mock socket.io with Angular and Jasmine

Issue I’m having trouble figuring out how to correctly mock Socket.io in an Angular application using Jasmine and Karma. Here are the files found in karma.conf.js: ‘bower_components/angular/angular.js’, ‘bower_components/angular-mocks/angular-mocks.js’, ‘bower_components/angular-ui-router/release/angular-ui-router.js’, ‘bower_components/angular-socket-io/socket.js’, ‘bower_components/socket.io-client/socket.io.js’, ‘bower_components/angular-socket-io/mock/socket-io.js’, ‘public/javascripts/*.js’, ‘ng_tests/**/*.js’, ‘ng_tests/stateMock.js’ Here is how my controller

Continue reading

Jasmine/Angular: TypeError: Cannot read property '$modules/$injector' of undefined

Issue I am trying to run jasmine test cases and i am landing on ‘$modules’ of undefined. I am loading unminified and latest versions of libraries as below, <script src=”lib/jquery-2.1.3.js”></script> <script src=”lib/jasmine-2.1.3.js”></script> <script src=”lib/jasmine-html.js”></script> <script src=”lib/boot.js”></script> <script src=”lib/angular-1.3.9.js”></script> <script src=”lib/angular-mocks-1.0.1.js”></script>

Continue reading

Jasmine – Testing if Controller Exists Getting Error

Issue i have a simple controller: app.controller(“RegisterController”, function ($scope, $location) { // Do something }); And all i am trying to do is to test this controller is defined: describe(‘RegisterController’, function() { var $rootScope, $scope, $controller; beforeEach(module(‘myApp’)); beforeEach(inject(function(_$rootScope_, _$controller_){ $rootScope

Continue reading

AngularJS and Jasmine: mocking services

Issue I am having trouble mocking the dependency of the following service “broadcaster” over the service “pushServices”. angular.module(‘broadcaster’, [‘pushServices’]); angular.module(‘broadcaster’).service(‘broadcaster’, [ ‘$rootScope’, ‘$log’, ‘satnetPush’, function ($rootScope, $log, satnetPush) { // .. contents .. }; }; The Jasmine spec is as

Continue reading

ngMockE2E is causing every request mock the moment added to dependency

Issue I have angular.module(“myModule”, [ //Some dependency “ngMockE2E” ]) This is actually mocking template request and throwing error Unexpected request: GET javascripts/custom/utils/templates/global_loader.html No more request expected at $httpBackend (angular-mocks.js?e_a_v=5:1226) at sendReq (angular.js?e_a_v=5:10215) at $get.serverRequest (angular.js?e_a_v=5:9927) at processQueue (angular.js?e_a_v=5:14437) at angular.js?e_a_v=5:14453

Continue reading

How to unit test (using Jasmine) a function in a controller which calls a factory service which returns a promise

Issue In the below SampleController, how do I unit test that postAttributes function calls sampleService.updateMethod. I’m having trouble since the updateMethod returns promise. angular.module(‘sampleModule’) .controller(‘SampleController’, SampleController); SampleController.$inject =[‘sampleService’]; function SampleController(sampleService){ this.postAttributes = function() { sampleService.updateMethod(number,attributes) .then(function(response){ //do something on successful

Continue reading

Angular services not being defined in beforeEach in Karma

Issue I have an Angular Application using Jasmine and Karma for testing. This is my testing class: var data = require(‘./user.mock.js’); describe(‘Service: UserService’, function () { var ServerUrl; var httpBackend; var userService; beforeEach(angular.mock.module(‘myModule’)); beforeEach(angular.mock.inject(function (_userService_, $httpBackend, _ServerUrl_) { userService =

Continue reading

Testing $interval in Jasmine/ Karma

Issue I have a simple factory angular.module(‘myApp.dice’,[]).factory(‘Dice’, [‘$interval’, function($interval){ return { rollDice: function(){ return $interval(function(c){ count++; }, 100, 18, false, 0); } }; }]); In my test case I have describe(‘rolling dice’, function(){ var promise, promiseCalled = false, notify =

Continue reading

Controller is undefined in jasmine test

Issue karma.config.js: module.exports = function(config) { config.set({ basePath: ”, frameworks: [‘jasmine’], files: [ ‘node_modules/angular/angular.min.js’, ‘node_modules/angular-mocks/angular-mocks.js’, ‘node_modules/angular-translate/dist/angular-translate.min.js’, ‘browser/javascripts/*.js’, ‘browser/tests/*.spec.js’ ], exclude: [], preprocessors: {}, reporters: [‘progress’], port: 9876, colors: true, logLevel: config.LOG_INFO, autoWatch: true, browsers: [‘Chrome’], singleRun: false, concurrency: Infinity })

Continue reading

How to test $scope in Jasmine test?

Issue I trying to write unit tests for Angularjs with Jasmine. Here is my controller: function HomeController($scope, fav, news, materials) { console.log(‘home controller’); $scope.testMe = true; } module.controller(‘HomeController’, HomeController); And tests describe(‘Home controller tests’, function() { var $rootScope, $scope, controller;

Continue reading

Unit test controller

Issue I make an ionic app and it finish but when i start to add tests to it I face a problem with $resources ,in this case I have this Controller : .controller(‘newAccountCtrl’, function($scope, $window, $rootScope, API, $ionicPopup, $state) {

Continue reading

karma test returning validateAmount is not a function

Issue I’m trying to test scope function which will check due amount, but while running the test I’m getting validateAmount is not a function. app.js var ManagmentApp = angular.module(“ManagemntApp”, [‘ngRoute’, ‘angularModalService’, ‘ng-fusioncharts’]); ManagmentApp.config([‘$routeProvider’, function ($routeProvider){ $routeProvider.when(‘/’, { templateUrl: ‘templates/CandidateForm.html’, controller:

Continue reading

Unit test $mdDialog angular material

Issue I called one $mdDialog inside a function. I want to unit-test $mdDialog ok and cancel cases. The below is my controller code (app.controller.js). (function () { ‘use strict’; app.controller(‘AppCtrl’, AppCtrl); AppCtrl.$inject = [‘$scope’, ‘$mdDialog’]; function AppCtrl($scope, $mdDialog) { $scope.saveEntry

Continue reading

Getting a `$digest already in progress` error when testing a `catch()` error handler

Issue This is my test: it(‘add.user() should POST to /users/, failure’, function() { mockBackend.expectPOST(“/users/”, {username:’u’, password: ‘p’, email: ‘e’, location: ‘loc’}).respond(400, {msg: “bad request”}); BaseService.add.user({username:’u’, password: ‘p’, email: ‘e’, location: ‘loc’}); mockBackend.flush(); }); afterEach(function() { mockBackend.verifyNoOutstandingExpectation(); mockBackend.verifyNoOutstandingRequest(); }); And when

Continue reading

Mock back-end responses in Angular 2

Issue In my application made with Angular 1, I’ve used angular-mocks to build front-end without requiring the back-end to be up and running: (function() { angular .module(‘myapp’) .run([‘$httpBackend’, function($httpBackend) { $httpBackend.whenGET(/.*\/api\/ratings\/\?.*/).respond(function(method, url) { var params = matchParams(url.split(‘?’)[1]); var list =

Continue reading