karma-jasmine

Angular 7 testing retryWhen with mock http requests fails to actually retry

做~自己de王妃 提交于 2020-03-20 16:03:05
问题 I have the following interceptor that tries to use a OAuth refresh_token whenever any 401 (error) response is obtained. Basically a refresh token is obtained on the first 401 request and after it is obtained, the code waits 2,5 seconds. In most cases the second request will not trigger an error, but if it does (token couldn't be refreshed or whatever), the user is redirect to the login page. export class RefreshAuthenticationInterceptor implements HttpInterceptor { constructor( private router

Type assertion using the '<>' is forbidden,use 'as' instead?

青春壹個敷衍的年華 提交于 2020-03-16 09:18:37
问题 I have the following test case in my spec, it (should create,()=>{ const url = (<jasmine.spy> http.get).calls.args(0)[0] expect(url).toBe('/api/get') }); When I run it, I am getting the following lint error. Type assertion using the '<>' is forbidden, use as instead? Can anyone please suggest help. 回答1: From Type Assertion. Type assertions as foo vs. <foo> Originally the syntax that was added was <foo> . This is demonstrated below: var foo: any; var bar = <string> foo; // bar is now of type

Testing FileSaver in Angular 5

不想你离开。 提交于 2020-03-02 00:28:54
问题 I have imported file-saver in angular 5 application, when I am executing test cases it is giving following error: TypeError: FileSaver.saveAs is not a function Spec.ts: import FileSaver from 'file-saver'; .ts import FileSaver from 'file-saver'; FileSaver.saveAs(blob, filename); How to mock filesaver in test cases. 回答1: This works in Angular 7, so I'm guessing it will work in NG5. The first thing is to import FileSaver using the 'as' syntax in both the component and the spec: import * as

Cannot find module in .spec.ts file in Ionic 3

坚强是说给别人听的谎言 提交于 2020-02-25 02:21:25
问题 I am working on Ionic 3 (Angular type) application, and to implement unit testing ,I am using Jasmine and Karma . Project structure of Ionic 3 (Angular type) doesnot provide .spec.ts file for pages/components/providers. So, I, manually, created .spec file for pages, but it is showing 'Cannot find module '@angular/core' and 'Cannot find module '@angular/core/testing' error. As these file are not able to fetch modules or not able to get the path to reach to those modules. I have tried following

Can't run Karma with Angular 1.4.9 with error angular.module(…).info is not a function

限于喜欢 提交于 2020-01-25 23:22:53
问题 When running karma start --single-run there is an error Chrome 62.0.3202 (Linux 0.0.0) ERROR Uncaught TypeError: angular.module(...).info is not a function at /home/ali/bedopedia/client/node_modules/angular-mocks/angular-mocks.js:80 When I opened anguler-mocks.js there is a function called info and it need angular 1.6 .info({ angularVersion: '1.6.6' }) here is package.json file "devDependencies": { "angular-mocks": "^1.6.0", "grunt": "^0.4.5", "grunt-jsmeter": "^0.3.1", "jasmine-core": "^2.5

Unit testing and mocking a service with DI

隐身守侯 提交于 2020-01-24 10:01:05
问题 I have been struggling with this for a while, and I'm hoping someone can help. I have a component that uses a service to get data. I am attempting to add unit tests to it. My problem is that the tests always fail with "Error: No provider for Http". Here is my code: Service: import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import { Observable } from 'rxjs/Rx'; import 'rxjs/add/operator/map'; import { Contact } from './contact.model'; @Injectable() export class

Karma's base directory location

一个人想着一个人 提交于 2020-01-24 05:41:05
问题 Unable to load template fixtures for my tests while using karma. To simplify, Went to c:\ Created a 1.txt text file. Made a simple karma init file containing: basePath: '', Started karma using: C:\> karma start .\sample.conf.js Chrome opened up at: http://localhost:9876/?id=49209467 I then tried to navigate to: http://localhost:9876/base/1.txt but got a "NOT FOUND" error message in the browser, and a message from karma: WARN [web-server]: 404: /base/1.txt What am I missing here? 回答1: Found

Jasmine unit test for angular service In controller

£可爱£侵袭症+ 提交于 2020-01-23 13:17:09
问题 I'm new to jasmine framework. I've gone through some tutorials and learned and started writing unit tests. 'facing one issue here is the description. I have a controller where i can invoke a service call to get the data. See the code below. $scope.getEmpInfo = function() { EmpService.getInfo($scope.empid) .then(function(data) { $scope.empData = data; $scope.populateEmpData($scope.empData); }, function(reason) { //do nothing } } Now, i want to write a unit test for the above method. Im able to

Angular - Karma - ngrx - No provider for Store

半城伤御伤魂 提交于 2020-01-23 07:22:42
问题 In one of my unit tests, I'm trying to mock @ngrx/store. I've used the technique successfully in another spec file, but when trying to use it in this one, I'm getting an injection error saying No provider for Store! Below is the relevant code from the spec file: beforeEach(async(() => { const emptyState = { opportunities: { list: { items: [], page: 1, total: 0 } } }; const mockStore = new MockStore<MockAppState>(emptyState); TestBed.configureTestingModule({ declarations: [

Jasmine - How to test a stylesheet directive

岁酱吖の 提交于 2020-01-16 20:36:16
问题 I have written a directive that interrogates the current URL, and loads the corresponding freestyle. The problem i am facing is how can i test the link function: HTML: <html lang="en" ng-app="myApp"> <head> <meta charset="UTF-8"> <title>CSS Test</title> <link get-style-directive /> </head> Directive: app.directive('getStyleDirective', ["$compile", "$location", function($compile, $location) { return { replace: true, scope: true, link: function(scope, element, attrs) { var urlValue = $location