jasmine

Angular promise not resolving in jasmine

断了今生、忘了曾经 提交于 2019-12-01 04:42:28
I have the following jasmine test: it('should resolve promise', inject(function ($q, $rootScope) { function getPromise(){ var deferred = $q.defer(); setTimeout(function(){ deferred.resolve(true); }, 1000); return deferred.promise; } var p = getPromise(); var cb = jasmine.createSpy(); runs(function(){ expect(cb).not.toHaveBeenCalled(); p.then(cb); $rootScope.$apply(); }); waitsFor(function(){ return cb.callCount == 1; }); runs(function(){ expect(cb).toHaveBeenCalled(); $rootScope.$apply(); }); })); I thought $rootScope.$apply was supposed to resolve all outstanding promises, but somehow it does

Angular 4 unit testing with jasmine /karma with http post mocking - how to fix

六眼飞鱼酱① 提交于 2019-12-01 04:15:19
I have a service I want to unit test in angular 4 typescript jasmine. Now, the http is doing a post , and it returns an identity, however.. it is not sending anything. I want to just have good code coverage but i don't understand how to quite complete this mocking statement. here is the method for http post in my service file addSession() { let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); return this.http.post(this.url, JSON.stringify({}), options) .map((response: Response) => response.json()); } Then the SPEC FILE ,

Argument 'fn' is not a function, when trying to do unit test with Jasmine and AngularJS

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 03:45:58
I'm trying to do some unit tests with Jasmine in my Angular application, but I'm facing some errors. Error Error: [$injector:modulerr] Failed to instantiate module LocalStorageModule due to: Error: [ng:areq] Argument 'fn' is not a function, got string Spec describe("testing the controller", function () { var $controllerConstructor; var scope; beforeEach(module('app', ['ngRoute', 'LocalStorageModule'])); beforeEach(inject(function ($controller, $rootScope) { $controllerConstructor = $controller; scope = $rootScope.$new(); })); it("should validate a contact", function () { var ctrl =

Testing backend API via $http in AngularJS/karma/jasmine tests?

依然范特西╮ 提交于 2019-12-01 03:43:55
How do I test my API backend using AngularJS/karma/jasmine tests? I have tried to create the smallest test-case showing my error: echo_server.py from bottle import response, route, run @route('/echo/<echo>') def echo_echo(echo): response.headers['Access-Control-Allow-Origin'] = '*' return {'echo': echo} # Served as JSON if __name__ == '__main__': run() test/unit/apiSpec.js // Should this be in `test/e2e/apiSpec.js`? describe('echo', function () { var scope, http; beforeEach(inject(function ($rootScope, $http) { $http.defaults.useXDomain = true; // CORS scope = $rootScope.$new(); http = $http;

Failed: Can't resolve all parameters for MatDialogRef: (?, ?, ?). unit testing Angular project

╄→尐↘猪︶ㄣ 提交于 2019-12-01 03:22:07
I am new to angular development and more new towards unit testing using jasmine. I have created a component to sow a dialog using angular material MatDialogRef, MAT_DIALOG_DATA from @angular/material. The component is working fine but the Unit testing is giving me an error which i am not able to resolve. I really need this to work and any help would be appreciated.... Thanks in advance..!!! Please find my code below: app.module.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { HttpClientModule, HttpClient }

Unit test angular right-click directive

天大地大妈咪最大 提交于 2019-12-01 03:15:18
问题 I want to write a Jasmine unit test for an AngularJS directive. The directive simply binds a contextmenu event handler function to the element: var myDirectives = angular.module('myApp.directives', []); myDirectives.directive('myRightClick', ['$parse', function ($parse) { return function (scope, element, attrs) { var fn = $parse(attrs.myRightClick); element.bind('contextmenu', function (event) { scope.$apply(function () { event.preventDefault(); fn(scope, { $event: event }); }); }); }; }]);

Angular promise not resolving in jasmine

梦想与她 提交于 2019-12-01 03:06:01
问题 I have the following jasmine test: it('should resolve promise', inject(function ($q, $rootScope) { function getPromise(){ var deferred = $q.defer(); setTimeout(function(){ deferred.resolve(true); }, 1000); return deferred.promise; } var p = getPromise(); var cb = jasmine.createSpy(); runs(function(){ expect(cb).not.toHaveBeenCalled(); p.then(cb); $rootScope.$apply(); }); waitsFor(function(){ return cb.callCount == 1; }); runs(function(){ expect(cb).toHaveBeenCalled(); $rootScope.$apply(); });

Jasmine 2.0 SpecRunner vs Karma

≯℡__Kan透↙ 提交于 2019-12-01 03:05:21
I just started using Jasmine and I was able to use the SpecRunner from the Html just fine. However when I configured Karma I encountered a discrepancy: describe('Calculator', function(){ var obj; beforeEach(function(){ //initialize object obj = new Object(); this.addMatchers({ toBeFive: function () { return { compare: function (actual, expected) { return { pass: actual === 5, message: actual + ' is not exactly 5' } } }; }, This piece of code does not work from the SpecRunner.html: this.addMatchers({ Instead I had to use this: jasmine.addMatchers({ This is what is include the specrunner: <!--

how to mock $window to unit test AngularJS service?

不羁岁月 提交于 2019-12-01 02:49:50
问题 Here is my service: var services = angular.module('amdotcom.services', ['ngResource']); services.factory('homePageRes', ['$resource', '$window', function ($resource, $window) { var wdw = angular.element($window); return $resource('home/index', { height: wdw.height(), width: wdw.width() }); }]); services.factory('homePageLoader', ['homePageRes', '$q', function (homePageRes, $q) { return function () { var delay = $q.defer(); homePageRes.get(function (homePage) { delay.resolve(homePage); },

Protractor: Find Element by Attribute

家住魔仙堡 提交于 2019-12-01 02:46:16
I have the following element i need to find for testing: <div class="alert alert-danger" role="alert" ng-show="notValid">Zugangsdaten eingeben</div> How can i find this element to check visibility (ng-show)? The ng-show attribute and value are the only attribute and value to identify the element uniquely. The class is used in many elements... I am searching for something like: var notValid = element(by.Attribute('ng-show', 'notValid'); You can find it by.css() : element(by.css('div[ng-show=notValid]')); $('div[ng-show=notValid]'); // shortcut for the above expression Or, by.xpath() : element