jasmine

How to test an Angular controller with a function that makes an AJAX call without mocking the AJAX call?

為{幸葍}努か 提交于 2019-12-12 09:52:35
问题 I have a function in a controller that calls a service which in turn calls another service, which makes the AJAX call and returns the promise. To put it simply: MainController call = function(){ //Handle the promise here, update some configs and stuff someService.call().then(success, failure); } SomeService return { call: function(){ return SomeOtherService.call(); } } SomeOtherService return { call: function(){ return $http.get(someUrl); } } How do I test the main controller's call function

Angularjs - simulate touch event in Protractor e2e test

允我心安 提交于 2019-12-12 09:37:12
问题 I use Protractor with Jasmine for my mobile Angularjs app. I want to test a touch event (touchStart / touchEnd etc...) on a particular element. Something like: it('should play video', function(){ var poster = by.css('.video-poster'); element(poster).??? //Simulate touch event here }); 回答1: Update: Since Protractor returns a Selenium element finder not an angular element, you'll have to use the executeScript() function to call a JavaScript method on it, like: var poster = element(by.css('

“ng e2e” is failling probably in reason of proxy but “ng serve” and “ng test” are working

雨燕双飞 提交于 2019-12-12 09:05:53
问题 I download https://github.com/blizzerand/pastebin-angular and I can successfully run "ng test" or "npm run test" (I understand both do exactly the same). C:\_pocs\ws_vsc\pastebin-angular-master>npm run test > test-angulr@0.0.0 test C:\_pocs\ws_vsc\pastebin-angular-master > ng test 10% building modules 1/1 modules 0 active29 12 2017 18:13:27.927:WARN [karma]: No captured browser, open http://localhost:9876/ 29 12 2017 18:13:27.998:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0

How to install and run npm jasmine locally

删除回忆录丶 提交于 2019-12-12 08:27:10
问题 Installing some npm packages globally is evil sometimes. I don't want to install jasmine like that: npm install -g jasmine How can I install and use jasmine without -g attribute? 回答1: 1) You need to init an npm project. On the 5-th step of the wizard (question test command: ) input jasmine npm init 1b) If you init npm project before, make sure you have these lines in your package.json "scripts": { "test": "jasmine" }, 2) Install jasmine as a local dependency npm i --save-dev jasmine 3) To

How can I test a $scope.$on event in a Jasmine test?

限于喜欢 提交于 2019-12-12 08:25:23
问题 I am unit testing a controller and I want to test an event handler. Say my controller looks like: myModule.controller('MasterController', ['$scope', function($scope){ $scope.$on('$locationChangeSuccess', function() { $scope.success = true; }); }]); Would I broadcast that in my Jasmine test? Would I emit it? Is there an accepted standard? 回答1: The solution I came up with is as follows: describe('MasterController', function() { var $scope, $rootScope, controller, CreateTarget; beforeEach

Get compiled template for controller in unit test

回眸只為那壹抹淺笑 提交于 2019-12-12 08:14:51
问题 I have the following controller: angular.module('app').controller('userList' , ['$scope','appRules',function ($scope,appRules) { $scope.isUserInRole=function(user,role){ console.log("exucuting userIsInRole:",user,role);//never happens return appRules.userIsInRole(user,role); } window.sscope=$scope; console.log($scope.menu); }]); This is used for the following route: angular.module('app', [,'ui.bootstrap','ngRoute']) .config(['$routeProvider','$locationProvider' ,function($routeProvider,

One of strings in array to match an expression

柔情痞子 提交于 2019-12-12 07:41:48
问题 The Problem: I have an array of promises which is resolved to an array of strings . Now the test should pass if at least one of the strings matches a regular expression. Currently, I solve it using simple string concatenation: protractor.promise.all([text1, text2, text3]).then(function (values) { expect(values[0] + values[1] + values[2]).toMatch(/expression/); }); Obviously, this does not scale well and is not particularly readable. The Question: Is is possible to solve it using a custom

Protractor: wait method isn't work

怎甘沉沦 提交于 2019-12-12 07:19:40
问题 I try to use wait() method instead sleep(), but it isn't working. I had code: browser.actions().click(filter_field).perform(); browser.sleep(3000); if (baloon_info.isPresent()) { //some expections } else { expect(true).toBe(false); } Now I want to do something like: var present_pri = browser.wait(function () { return balloon_info.isPresent(); }, 3000); if (present_pri) { //some expections } else { expect(true).toBe(false); } But if balloon isn't present I have the error message: Wait timed

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

Unit testing Angular directive click handler

陌路散爱 提交于 2019-12-12 05:30:43
问题 I've got a directive that adds a click handler to an element: module.directive('toggleSection', ['$timeout', function ($timeout) { return { restrict: 'A', link: function (scope, element, attrs) { element.bind('click', function (event) { scope.$apply(function () { var scopeProp = 'show' + attrs.toggleSection; event.preventDefault(); event.stopPropagation(); scope[scopeProp] = !scope[scopeProp]; return false; }); }); } }; }]); When the element is clicked, it toggles another property on the