Per*_* Ch 2 unit-testing jasmine typescript angular angular11
我想测试将我发送到特定页面的方法。我检查了几个示例,所有示例都以与我相同的方式完成,但我收到错误:
预期间谍导航已被调用: [ '/themen' ] 但它从未被调用。错误:预期间谍导航已被调用:[ '/themen' ] 但它从未被调用。
我的html代码是:
<button class="arrow-back-icon kf-tooltip-btn align-middle" id="backButton"
tabindex="0" name="ic_arrow-back_24" (click)="goBack()">
</button>
Run Code Online (Sandbox Code Playgroud)
我的组件中的方法是:
import {AfterViewChecked, ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {SharingDataService} from '../services/sharing-data.service';
import {ActivatedRoute, Router} from '@angular/router';
import {BreakpointObserver} from '@angular/cdk/layout';
@Component({
selector: 'app-meeting',
templateUrl: './meeting.component.html',
styleUrls: ['./meeting.component.scss']
})
export class MeetingComponent implements OnInit, AfterViewChecked {
constructor(private readonly translate: TranslateService,
private readonly sharingDataService: SharingDataService,
private readonly router: Router,
private readonly activatedRoute: ActivatedRoute,
private readonly breakpointObserver: BreakpointObserver,
private readonly cd: ChangeDetectorRef) {
}
goBack(): void {
this.router.navigate(['/themen']).then();
}
}
Run Code Online (Sandbox Code Playgroud)
测试是:
import {ComponentFixture, fakeAsync, inject, TestBed, waitForAsync} from '@angular/core/testing';
import {MeetingComponent} from './meeting.component';
import {Router, Routes} from '@angular/router';
import {CommonModule} from '@angular/common';
import {RouterTestingModule} from '@angular/router/testing';
import {TranslateLocalModule} from '../../../projects/shared/src/lib/translate';
describe('MeetingComponent', () => {
let sut: meetingComponent;
let fixture: ComponentFixture<MeetingComponent>;
const routes: Routes = [
{path: '', redirectTo: 'themen', pathMatch: 'full'},
{path: 'meeting', component: meetingComponent},
{path: '**', redirectTo: 'themen', pathMatch: 'full'}
];
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ MeetingComponent ],
imports: [
CommonModule,
RouterTestingModule.withRoutes(routes),
TranslateLocalModule,
providers: [
]
}).compileComponents();
});
beforeEach(() => {fixture = TestBed.createComponent(MeetingComponent);
sut = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(sut).toBeTruthy();
});
it('should go on previous step', waitForAsync(() => {
const routerSpy = {
navigate: jasmine.createSpy('navigate')
};
sut.onPrevStep();
fixture.detectChanges();
expect (routerSpy.navigate).toHaveBeenCalledWith('/themen');
}));
});
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我做错了什么吗?
我也尝试过这个:
it('should navigates to previous step', fakeAsync(async () => {
spyOn(sut, 'onPrevStep').and.returnValue(await Promise.resolve());
const navigateSpy = spyOn(router, 'navigate');
sut.onPrevStep();
tick();
expect(sut.onPrevStep).toHaveBeenCalled();
expect(navigateSpy).toHaveBeenCalledWith(['/themen']);
}));
Run Code Online (Sandbox Code Playgroud)
但错误是一样的。
谢谢!
是的,我认为你的routerSpy与路由器没有关联。
关注评论!!
尝试这个:
import {ComponentFixture, fakeAsync, inject, TestBed, waitForAsync} from '@angular/core/testing';
import {MeetingComponent} from './meeting.component';
import {Router, Routes} from '@angular/router';
import {CommonModule} from '@angular/common';
import {RouterTestingModule} from '@angular/router/testing';
import {TranslateLocalModule} from '../../../projects/shared/src/lib/translate';
describe('MeetingComponent', () => {
let sut: meetingComponent;
let fixture: ComponentFixture<MeetingComponent>;
// !! add router variable for later use !!
let router: Router;
const routes: Routes = [
{path: '', redirectTo: 'themen', pathMatch: 'full'},
{path: 'meeting', component: meetingComponent},
{path: '**', redirectTo: 'themen', pathMatch: 'full'}
];
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ MeetingComponent ],
imports: [
CommonModule,
RouterTestingModule.withRoutes(routes),
TranslateLocalModule,
providers: [
]
}).compileComponents();
});
beforeEach(() => {
// !! get a handle on the router !!
router = TestBed.inject(Router);
fixture = TestBed.createComponent(MeetingComponent);
sut = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(sut).toBeTruthy();
});
it('should go on previous step', waitForAsync(() => {
// !! spy on the router !!
spyOn(router, 'navigate');
// !! I think this should be sut.goBack(); !!
sut.onPrevStep();
fixture.detectChanges();
// !! change expectation !!
expect(router.navigate).toHaveBeenCalledWith(['/themen']);
}));
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13163 次 |
| 最近记录: |