具有多种路由功能的 Angular Jasmine 测试组件
我正在尝试为我的组件之一编写测试。它有两个功能可以重新路由到另一个页面。我编写了两个测试,每个测试都单独工作,但由于某种原因,当两者同时出现在代码中时,它们会发生冲突。我的间谍在每次测试之间不会重置吗?我曾尝试过设立单独的间谍,但没有成功。
county-resources.component.ts
routeToNewPage(){
this.router.navigate(['/admin/new-agency'], {
state: {
counties: this.countiesList,
categories: this.categories
}
});
}
routeToEditPage(data:any){
this.router.navigate(['/admin/edit-agency'], {
state: {
counties: this.countiesList,
categories: this.categories,
data: data
}
});
}
component.spec.ts
describe('CountyResourcesComponent', () => {
let component: CountyResourcesComponent;
let fixture: ComponentFixture<CountyResourcesComponent>;
let routerSpy = {navigate: jasmine.createSpy('navigate')};
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ CountyResourcesComponent ],
providers: [
ApiService,
{
provide: Router, useValue: routerSpy
}
],
imports: [
ReactiveFormsModule,
HttpClientTestingModule,
RouterTestingModule
],
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(CountyResourcesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it(`should reroute page to new-agency`, () => {
component.routeToNewPage();
const navArgs = routerSpy.navigate.calls.first().args[0];
expect (navArgs).toEqual(['/admin/new-agency']);
});
it(`should reroute page to edit-agency`, () => {
let data = {
junk: 12,
junk2: 34
}
component.routeToEditPage(data);
const navArgs = routerSpy.navigate.calls.first().args[0];
expect (navArgs).toEqual(['/admin/edit-agency']);
});
});
I am trying to write tests for one of my components. It has two functions in it that reroute to another page. I wrote two tests, and each of them works individually, but for some reason they clash when both are in the code at the same time. Does my spy not reset between each test? I've tried making separate spies, but that didn't work.
county-resources.component.ts
routeToNewPage(){
this.router.navigate(['/admin/new-agency'], {
state: {
counties: this.countiesList,
categories: this.categories
}
});
}
routeToEditPage(data:any){
this.router.navigate(['/admin/edit-agency'], {
state: {
counties: this.countiesList,
categories: this.categories,
data: data
}
});
}
component.spec.ts
describe('CountyResourcesComponent', () => {
let component: CountyResourcesComponent;
let fixture: ComponentFixture<CountyResourcesComponent>;
let routerSpy = {navigate: jasmine.createSpy('navigate')};
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ CountyResourcesComponent ],
providers: [
ApiService,
{
provide: Router, useValue: routerSpy
}
],
imports: [
ReactiveFormsModule,
HttpClientTestingModule,
RouterTestingModule
],
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(CountyResourcesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it(`should reroute page to new-agency`, () => {
component.routeToNewPage();
const navArgs = routerSpy.navigate.calls.first().args[0];
expect (navArgs).toEqual(['/admin/new-agency']);
});
it(`should reroute page to edit-agency`, () => {
let data = {
junk: 12,
junk2: 34
}
component.routeToEditPage(data);
const navArgs = routerSpy.navigate.calls.first().args[0];
expect (navArgs).toEqual(['/admin/edit-agency']);
});
});
This error is referencing the line with the "expect" in the "should reroute page to edit-agency" test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是正确的,您的间谍不会在每个测试之间重置。
如果我是你,我会进行以下更改(关注!!):
You're correct, your spies don't reset between each test.
If I were you, I would make the following changes (follow !!):