karma-jasmine

Angular - mock Promise method for Jasmine unit test

别来无恙 提交于 2021-02-04 17:08:25
问题 Method to test public onSubmit(registerData: RegisterDataModel): void { this.registrationService.registerWithEmailAndPassword(registerData).then((msg: string[]) => this.router.navigate(['/completeSignUp']).then(() => { msg.forEach(singleMessage => this.notificationService.primary(singleMessage)); })) .catch((msg) => msg.forEach(singleMessage => { this.notificationService.danger(singleMessage); })); } I want to test if router.navigate is called in my method. Now I want to mock my service

How to unit test an Angular component that is making GET call using a service

拈花ヽ惹草 提交于 2021-01-29 14:58:52
问题 This question here: How to unit test a component that calls observable service has an accepted answer. But I don't want to test the service right now. I've some articles stored on a server. I've a component ArticlesComponent which uses ArticlesService to fetch all those articles. Today I want to test ArticlesComponent only. Here is the code. articles.component.ts import { Component, OnInit } from '@angular/core'; import { ArticleService } from '../article.service'; ... @Component({ ... })

How to mock HTTP Request in Angular?

时间秒杀一切 提交于 2021-01-29 10:39:17
问题 I have checked a lot of articles and answers but I don't seem to find the right way to mock HTTP Requests for my methods. I want to test my frontend application independently from the backend . Here is the type of methods I have: private getProfile() { this.http .get('go/profile/get', {withCredentials: true}) .subscribe((profile: Profile) => { this.user.profile = profile; this.updateLineMsgs(); }); } Any suggestions ? 回答1: Usually i mock my Http requests with HttpClientTestingModule : import

Angular 10 testing ViewContainerRef's injector returns undefined

∥☆過路亽.° 提交于 2021-01-29 08:11:18
问题 The problem happens after migrating our project from Angular 8.2.14 to Angular 10.2.24. This is the test code fdescribe('PopupModalService Testing', () => { let componentFactoryResolver: ComponentFactoryResolver; let viewContainerRef: ViewContainerRef; let popupModalService: PopupModalService; beforeEach(waitForAsync(() => { const viewContainerRefSpy = jasmine.createSpyObj('ViewContainerRef', ['insert']); TestBed.configureTestingModule({ declarations: [ PopupModalComponent, DialogApiComponent

How to unit test a component to check a particular component is rendered or not

僤鯓⒐⒋嵵緔 提交于 2021-01-28 14:22:36
问题 I've a very simple custom component. I call it NavbarComponent and the selector is app-navbar . It is a very basic static navbar. I'm rendering it on app.component.html : app.component.html <app-navbar></app-navbar> <router-outlet></router-outlet> I'm writing a unit test case like this: app.component.spec.ts import { TestBed, async } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { AppComponent } from './app.component'; import {

Why am I getting a “Failed: Cannot read property 'subscribe' of undefined” while running tests?

谁说胖子不能爱 提交于 2021-01-28 11:17:51
问题 I am relatively new to testing in Angular2+ and am setting up my files for testing. Since building the project, I've written a lot of code and am now trying to test parts of it. However, lots of those tests are now broken so I am now trying to delete the unneeded default tests or fix the ones that are useful. This particular test won't run because it fails before it hits the It() testing method. I'm not sure what I'm missing, any help would be appreciated. I am receiving this error: Failed:

Cannot read property 'textContent' of null Angular Testing

元气小坏坏 提交于 2021-01-28 08:43:51
问题 I have simple angular application with AppComponent and ProductComponent . When I run ng test command, getting below error on http://localhost:9876/?id=80860281 Jesmine 3.5.0Options finished in 0.473s 4 specs, 1 failure, randomized with seed 16102 Spec List | Failures AppComponent > should render title TypeError: Cannot read property 'textContent' of null at <Jasmine> at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/app.component.spec.ts:33:51) at ZoneDelegate.invoke

Test angular snackbar

﹥>﹥吖頭↗ 提交于 2021-01-28 05:24:02
问题 I am making a simple snackbar for which code as follows, app.component.ts: ngOnInit(){ this.dataService.valueChanges.pipe( filter((data) =>data=== true), switchMap(() => { const snackBarRef = this.matSnackBar.open( 'A new value updated', 'OK', { duration: 3000 } ); return snackBarRef.onAction(); }) ) .subscribe(() => { this.window.location.reload(); }); } app.component.spec.ts (Including mock data for service) describe('AppComponent', () => { let component: AppComponent; let fixture:

ngx-translate instant function is not a function thrown: Unit testing

邮差的信 提交于 2021-01-27 18:19:17
问题 I'm fairly new for unit testing and Angular, and i'm trying to write some unit tests for my project. The error i'm getting is; Uncaught TypeError: _this.translate.instant is not a function thrown Expected null to be truthy. The imports of the component.spec file includes beforeEach(async(() => { TestBed.configureTestingModule({ imports:[ ClarityModule, RouterTestingModule, TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: (http: HttpClient) => new TranslateHttpLoader

Angular 2 App Testing, how to access and operate on html elements?

廉价感情. 提交于 2021-01-27 12:29:54
问题 I have an Angular 2 Frontend to be tested, I found on a Site ( https://kendaleiv.com/angular-2-component-testing-template-using-testbed/ ) a guy using ng-test (angular core testing) TestBed, this is a really basic example it does not explain how to do operations with the Elements. My App has an Input Form element, once the input signal has been sent through the Form Element it show a results set on an InfoBox element. So what I would like to do is to retrieve the element Form from the TestBed