jasmine

Protractor with jasmine does not show stack traces to line with error

岁酱吖の 提交于 2021-01-29 07:09:37
问题 I've got a protractor test that does this: it('Test', async () => { throw new Error() } When this errors, it outputs this: ✗ Test - Failed: Error at callWhenIdle (.../node_modules/jasminewd2/index.js:62:5) at .../node_modules/jasminewd2/index.js:127:13 at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) (node:43369) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): Error: EPIPE write EPIPE How do I get a useful stack trace that says the

Error: <toHaveBeenCalled> : Expected a spy, but got undefined

谁都会走 提交于 2021-01-29 06:18:23
问题 I am getting an error expected Spy but got undefined in my angular 10 test. I am using Jasmine and Karma. I have mocked the service SpreadsheetService as mockSpreadSheetService. I am calling the its method. I fail to understand why its expecting a spy. One possible reason i could think of is that mockSpreadSheetService gets created before running the test and we are calling component.ngOnit which possible recreates the component instance. I tried to explicitly create the mockservice after

Why checkbox checked property doesn't change after detectChanges()

一个人想着一个人 提交于 2021-01-29 04:00:34
问题 I try to change checkbox checked property by changing model but it doesn't work. I cannot understand this behavior. Template : <input type="checkbox" [(ngModel)]="model"/> Testing Code : it('should be checked after click on unchecked checkbox', () => { const checkbox: HTMLInputElement = fixture.nativeElement.querySelector('input'); component.model = true; fixture.detectChanges(); expect(checkbox.checked).toBeTruthy(); }); Full code: https://stackblitz.com/edit/angular-testing-checkbox?file

catchError always gets called in HTTP unit testing

拟墨画扇 提交于 2021-01-28 17:53:41
问题 I have a service that makes a HTTP call and I am trying to write tests for it. The method in the service that I am trying to test looks like this // my.service.ts setUserAgreement(accept: boolean): Observable<any> { const data = { accept }; return this.http.post<any>(this.url, data, this.getHttpHeader('1')) .pipe( tap(x => this.logHttp(x)), map(x => this.parseHttp(x)), catchError(this.handleErrorInternal('setUserAgreement')) ); } My test file looks like this import { TestBed, async } from '

catchError always gets called in HTTP unit testing

£可爱£侵袭症+ 提交于 2021-01-28 17:47:58
问题 I have a service that makes a HTTP call and I am trying to write tests for it. The method in the service that I am trying to test looks like this // my.service.ts setUserAgreement(accept: boolean): Observable<any> { const data = { accept }; return this.http.post<any>(this.url, data, this.getHttpHeader('1')) .pipe( tap(x => this.logHttp(x)), map(x => this.parseHttp(x)), catchError(this.handleErrorInternal('setUserAgreement')) ); } My test file looks like this import { TestBed, async } from '

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 {

Marble testing observable that changes after a method call?

淺唱寂寞╮ 提交于 2021-01-28 09:34:01
问题 In Angular 8, I have a service with a readonly Observable property, spawned from a BehaviorSubject<string> that contains a string describing the service's state. Also in the service are methods that change the state of the service. export class StateService { private _state = new BehaviorSubject<string>('before'); readonly state$ = this._state.asObservable(); constructor () { } begin() { this._state.next('during'); } finish() { this._state.next('after'); } reset() { this._state.next('before')

Jasmine context + Typescript

孤者浪人 提交于 2021-01-28 09:29:50
问题 I'm using Jasmine with Typescript and recently we started using the this context in beforeEach and it . Example: beforeEach(() => { this.fixture = TestBed.createComponent(blablabla); }); it('should do something', () => { expect(this.fixture).toBeTruthy(); }); The problem is that TypeScript is not smart enough to figure out that this inside beforeEach is exactly the same this as in it . Does anyone know an easy way to 'hint' typescript about this fact? Is this even possible? 回答1: You can

Jasmine context + Typescript

你离开我真会死。 提交于 2021-01-28 09:23:44
问题 I'm using Jasmine with Typescript and recently we started using the this context in beforeEach and it . Example: beforeEach(() => { this.fixture = TestBed.createComponent(blablabla); }); it('should do something', () => { expect(this.fixture).toBeTruthy(); }); The problem is that TypeScript is not smart enough to figure out that this inside beforeEach is exactly the same this as in it . Does anyone know an easy way to 'hint' typescript about this fact? Is this even possible? 回答1: You can

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