karma-jasmine

how can i create MockActivatedRoute for the parent routes of ActivatedRoute in angular 4 Unit testing

我的未来我决定 提交于 2019-12-11 07:19:39
问题 Lets say I have a component that access parent route like this: ngOnInit() { this.sub = this.route.parent.parent.parent.params.subscribe(params => { this.siteId = params["siteId"]; this.loadContents(); this.loadTypes(this.siteId); });} I m providing fake value for parent route like this: providers: [ SiteService, UserService, Modal, Overlay, { provide: ActivatedRoute, useValue: { parent: { parent: { parent: { 'params': Observable.from([{ 'siteId': '156' }]) } } } }, }, OverlayRenderer,

AngularJS unit test with $http request never fires .then() callbacks

北战南征 提交于 2019-12-11 06:59:29
问题 I'm trying to get unit tests running with mocked responses in separate json files. The tests were working when I used $q to return promises resolved manually in my OpsService, but when I tried to make them into actual $http requests to return the actual json files, they no longer worked. edit: I've tried $httpBackend.flush() , $rootScope.$apply() , and $rootScope.$digest() but none of those seem to resolve the promises. My service: OpsService.service('OpsService', function ($q, $http) { this

angular 2 MockBackend test http timeout?

久未见 提交于 2019-12-11 06:42:46
问题 Is there a way to test http timeout behaviour of a service? I'm using MockBackend even when setting job's timeout to 0 no logging of 'TIMEOUT' present. export class MyHttpService { private sendGetRequest (job: HttpJob): Observable<any> { return this.http.get(job.getUrl(), this.options) .share() .timeout(job.getTimeout(), () => this.requestTimeout(job)) .catch((err) => this.errorHandler(err, job)); }; private requestTimeout(job: HttpJob): Error { job.errorCode = ErrorCode.TIMEOUT; console.log(

Unit test an angular controller and service which uses a promise?

瘦欲@ 提交于 2019-12-11 06:37:31
问题 I cannot get the test result to pass I'm using a very basic implementation to understand testing deeper. I have a factory which returns a promise, accessed from my controller. I want to test that the call succeeds and assigns the response to the repos var. Following is the code: 'use strict'; angular.module('app') .factory('searchServ', function ($timeout, $q, $http) { return { fetch: function(user) { var deferred = $q.defer(); $timeout(function(){ $http({method: 'GET', url: 'https://api

Angular Directive Isolate Scope Unit Test Failed

雨燕双飞 提交于 2019-12-11 06:24:40
问题 I am trying to unit test an Angular Component with Karma Jasmine for the very first time. My index.html looks like : <body ng-app="heroApp"> <!-- components match only elements --> <div ng-controller="MainCtrl as ctrl"> <b>Hero</b><br> <hero-detail hero="ctrl.hero"></hero-detail> </div> </body> </html> And index.js looks like : (function(angular) { 'use strict'; angular.module('heroApp', []).controller('MainCtrl', function MainCtrl() { this.hero = { name: 'Miles Bronson' }; }); })(window

this.ProductDataService.getAllProducts is not a function using jasmine-karma

血红的双手。 提交于 2019-12-11 06:06:43
问题 I am running unit test-cases i.e. jasmine-karma in angular7. And I am getting this error - ProjectManagementComponent should use the ProjectList from the service TypeError: this.ProjectManagementService.getProject is not a function If instead of useValue , i use useClass , i get error - [object ErrorEvent] thrown I tried varied options , but unable to figure out over internet. app.component.spec.ts describe('ProjectManagementComponent', () => { let comp: ProjectManagementComponent; let

Unit test a service : AngularJS

吃可爱长大的小学妹 提交于 2019-12-11 06:01:58
问题 I have few questions with to write a proper unit test for a service using jasmine as framework and karma as test runner. here is what i implemented in example-service.js : export default class ExampleService { constructor($resource, $http, $urlMatcherFactory) { 'ngInject'; this.$resource = $resource; this.$http = $http; this.$urlMatcherFactory = $urlMatcherFactory; } exampleMethodOne() { //some code lines } exampleMethodTwo() { //some code lines } } ExampleService.selector = 'myExampleService

how to simulate keypress for unit testing in jasmine

大憨熊 提交于 2019-12-11 05:06:38
问题 I need to unit test function that is triggered when key pressed. public onKeyDown(event: KeyboardEvent): void { if (event.ctrlKey && event.keyCode === 38) { console.log('increase'); } if (event.ctrlKey && event.keyCode === 40) { console.log('decrease'); } /* Prevent entering characters */ if (event.keyCode >= 65 && event.keyCode <= 90) { return; } } How can I simulate keypress to satisfy the fist condition, for example? 回答1: The example code below shows how an event is created, triggered, and

jasmine angular 4 unit test router.url

梦想与她 提交于 2019-12-11 04:14:54
问题 I am unit testing a function in angular 4 project using jasmine which a switch statement like mentioned below: switch(this.router.url) { case 'firstpath': { // some code } break; case 'secondpath': { // some more code } break; default: break; } In my spec.ts file. I can't stub or change the value of router.url.I want my cases to execute but default is executing. I tried different ways to set or spyOn and return value, but everytime url is '/'. Every suggestion or solution will be welcomed.