jasmine

How to test on destroy scope

余生长醉 提交于 2021-02-06 15:31:27
问题 How to unit testing an $destroy event of a Directive in angularjs? I have the code in my directive: scope.$on('$destroy', function () { //clean something }); My test code: it('on destroy',function(){ scope.$destroy(); scope.$digest(); //expect everything done? }); Any suggestion! 回答1: You can select the DOM from the template of your directive and get the scope of it, then run $destroy(). Ex: your tpl: "<div id='tuna'></div>" your test: it('test on destroy', function(){ var isolateScope = $

Test a function that contains a setTimeout()

北城以北 提交于 2021-02-05 20:18:31
问题 I have a close function in my component that contains a setTimeout() in order to give time for the animation to complete. public close() { this.animate = "inactive" setTimeout(() => { this.show = false }, 250) } this.show is bound to an ngIf . this.animate is bound to an animation. I have a test that needs to test this function it("tests the exit button click", () => { comp.close() fixture.detectChanges() //verifies the element is no longer in the DOM const popUpWindow = fixture.debugElement

Test a function that contains a setTimeout()

谁说我不能喝 提交于 2021-02-05 20:12:10
问题 I have a close function in my component that contains a setTimeout() in order to give time for the animation to complete. public close() { this.animate = "inactive" setTimeout(() => { this.show = false }, 250) } this.show is bound to an ngIf . this.animate is bound to an animation. I have a test that needs to test this function it("tests the exit button click", () => { comp.close() fixture.detectChanges() //verifies the element is no longer in the DOM const popUpWindow = fixture.debugElement

Unit testing that items get filtered out of Observable (Jasmine/RxJS)

北战南征 提交于 2021-02-05 05:30:07
问题 I'm doing unit testing with Jasmine/Karma against an Angular service. I'd like to confirm that my service properly filters items. For example, if I have a service to get people over a certain age, it should return people over the minimum age (positive case) should NOT return people under a the minimum age (negative case) It's #2 that I'm struggling to test. The service: getPeople(minAge: number): Observable<string> { const source = Observable.from([ { name: 'Joe', age: 30 }, { name: 'Frank',

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

WebDriverError: no such session error using ChromeDriver Chrome through Selenium and Jasmine

别来无恙 提交于 2021-02-04 05:53:27
问题 I'm trying to make an automated test for my webpage and I'm using Jasmine in tandem with selenium. When testing on chrome (using chromedriver) I get, unpredictably, the error below. It happens frequently enough that when I run a test suite it hardly ever finishes. Ive found evidence of this bug but cant find a solid answer: https://bugs.chromium.org/p/chromedriver/issues/detail?id=732 (granted this was for chromium and I'm using chrome) WebDriverError: no such session (Driver info:

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 test the existence of a route in Angular using Jasmine?

六眼飞鱼酱① 提交于 2021-01-29 11:27:58
问题 I'm trying to write a test that checks for the existence of a route using Jasmine for Angular application: import { routes } from './app-routing.module'; import { UsersComponent } from './users/users.component'; describe('routes', () => { it('should contain route for /users', () => { const expectedRoute = { path: 'users', component: UsersComponent }; expect(routes).toContain(expectedRoute); }); }); but the test fails (probably because of object equality in Javascript How object equality

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

Where is npm run looking for the scripts?

微笑、不失礼 提交于 2021-01-29 07:35:57
问题 When I type "npm run test" in the command line, npm goes to package.json, to the "scripts" section and tries to match "test" there. So far so good. Now, the line behind "test" is the following: "JASMINE_CONFIG_PATH=./spec/support/jasmine.json jasmine-run" but the first part (everything except "jasmine-run") can be removed witout problems. [I have a similarily structured project where it works, so I can test these modifications] now: WHERE is npm looking for "jasmine-run" ??? Because since I