angular-mock

How to Jasmine test code within angular module run block

时光总嘲笑我的痴心妄想 提交于 2019-12-12 07:18:32
问题 I would like to Jasmine test that Welcome.go has been called. Welcome is an angular service. angular.module('welcome',[]) .run(function(Welcome) { Welcome.go(); }); This is my test so far: describe('module: welcome', function () { beforeEach(module('welcome')); var Welcome; beforeEach(inject(function(_Welcome_) { Welcome = _Welcome_; spyOn(Welcome, 'go'); })); it('should call Welcome.go', function() { expect(Welcome.go).toHaveBeenCalled(); }); }); Note: welcome (lowercase w) is the module

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

為{幸葍}努か 提交于 2019-12-12 04:49:10
问题 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) { $rootScope.$state = $state; $rootScope.$log = $log; $rootScope.global = Global; }]); controllers.js lama

How can I make $httpBackend insensitive to the order of URL query parameters?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 12:30:01
问题 I am using the Angular.js $httpBackend to test some services that wrap $http calls (this is in ngMock, not ngMockE2E). It seems that things like expect and when are sensitive to the order of URL query parameters. E.g. if I do $httpBackend.when('POST','/apiCall?X=1&Y=2').respond(/* ... */) or $httpBackend.expectPOST('/apiCall?X=1&Y=2') , I get URL mismatches if I have Y=2&X=1 in the URL instead of X=1&Y=2 . I want to write my tests in such a way that the service being tested will be free to

How to mock socket.io with Angular and Jasmine

北城以北 提交于 2019-12-10 10:50:31
问题 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',

How to test the config function of an Angular module?

旧街凉风 提交于 2019-12-10 02:56:54
问题 I'm defining some setup code in the config function of an Angular module that I want to unit test. It is unclear to me how I should do this. Below is a simplified testcase that shows how I'm getting stuck: 'use strict'; angular.module('myModule', []).config(['$http', '$log', function($http, $log) { $http.get('/api/getkey').then(function success(response) { $log.log(response.data); }); }]); describe('myModule', function() { it('logs a key obtained from XHR', inject(function($httpBackend) {

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

廉价感情. 提交于 2019-12-09 13:30:03
问题 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 () { return !isMobileDevice(); }, isMobile: function () { return isMobileDevice(); } };

How to automatically load a module in AngularJS without specifying it as a dependancy

╄→尐↘猪︶ㄣ 提交于 2019-12-08 05:23:28
问题 ngMock does some magic to automatically include itself if you include angular-mocks.js in your index.html. What's the simplest way to force angular to load a module in test mode simply by including a file and not having to edit any module dependencies. 回答1: The only way to load a module is by calling angular.module(...) . ngMocks loads "itself" by calling: angular.module('ngMock', ['ng']).provider(...).config(...); You don't need to declare a module as a dependency to load it. You can just

How to mock socket.io with Angular and Jasmine

心已入冬 提交于 2019-12-06 07:45:42
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 looks: var lunchrControllers =

How to test the config function of an Angular module?

老子叫甜甜 提交于 2019-12-05 03:45:56
I'm defining some setup code in the config function of an Angular module that I want to unit test. It is unclear to me how I should do this. Below is a simplified testcase that shows how I'm getting stuck: 'use strict'; angular.module('myModule', []).config(['$http', '$log', function($http, $log) { $http.get('/api/getkey').then(function success(response) { $log.log(response.data); }); }]); describe('myModule', function() { it('logs a key obtained from XHR', inject(function($httpBackend) { $httpBackend.expectGET('/api/getkey').respond(200, '12345'); angular.module('myModule'); $httpBackend

Angular mock fails to inject my module dependencies

扶醉桌前 提交于 2019-12-05 01:56:01
I want to test an Angular controller for my application fooApp , defined as follow: var fooApp = angular.module('fooApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]); ... The controller, MainCtrl is defined: "use strict"; fooApp.controller('MainCtrl', function ($scope, $rootScope, fooService) { ... } So I've tested several ways to create a test, like this one: 'use strict'; describe('MainController test', function () { var scope; var controller; beforeEach(function () { angular.mock.module('ngRoute', []); angular.mock.module('ngAnimate', []); angular.mock.module('hmTouchEvents', []); angular