gpt4 book ai didi

angular - 如何使用 Angular 中的查询参数对 'navigate' 进行单元测试?

转载 作者:行者123 更新时间:2023-12-04 12:25:36 26 4
gpt4 key购买 nike

这是我在组件中的方法。

 editThis(id) {
this.router.navigate(['/categories/edit'], { queryParams: { id: id } });
}

这是我的单元测试用例。
fit('should call navigate with correct params', () => {
component.editThis("5c7d5fde213e25232864dbe0");
expect(new MockRouter().navigate).toHaveBeenCalledWith(['/categories/edit'], { queryParams: { id: "5c7d5fde213e25232864dbe0" } });
});

这是模拟路由器。
class MockRouter {
navigateByUrl(url: string) { return url; }
navigate = jasmine.createSpy('navigate');

}

我收到此错误。

Expected spy navigate to have been called with [ [ '/categories/edit' ], Object({ queryParams: Object({ id: '5c7d5fde213e25232864dbe0' }) }) ] but it was never called.



你能给我建议一种方法来测试这个方法吗?

完整的测试代码。
import { FacadeService } from './../../../services/facade.service';
import { HttpClientModule } from '@angular/common/http';
import { AngularFontAwesomeModule } from 'angular-font-awesome';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { CategoriesViewComponent } from './categories-view.component';
import { RouterTestingModule } from '@angular/router/testing';
import { Observable, of } from 'rxjs';
import { Router } from '@angular/router';

var allCategories = [

{
"_id": "5c7d5fde213e25232864dbe0",
"name": "Politics",
"updatedAt": "2019-03-04T17:26:54.262Z",
"createdAt": "2019-03-04T17:26:54.262Z",
"__v": 0
}
];

class MockRouter {
navigateByUrl(url: string) { return url; }
navigate = jasmine.createSpy('navigate');

}

class MockedFacadeService {
getUserDataFromLocalStorage() {
return false;
}
getGuestPermissionsFromLocalStorage() {
return { "comments": { "create": false, "read": true, "update": false, "deleteAny": false, "delete": false }, "post": { "create": false, "read": true, "update": false, "delete": false, "like": false, "dislike": false }, "category": { "create": false, "read": true, "update": false, "delete": false } };
}
getCategories() {
return of(allCategories);
}
}

describe('CategoriesViewComponent', () => {
let component: CategoriesViewComponent;
let fixture: ComponentFixture<CategoriesViewComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [AngularFontAwesomeModule, RouterTestingModule, HttpClientModule],
declarations: [CategoriesViewComponent],
providers: [{ provide: FacadeService, useClass: MockedFacadeService },
{ provide: Router, useClass: MockRouter }]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(CategoriesViewComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

fit('should create', () => {
expect(component).toBeTruthy();
});

fit('should call navigate with correct params', () => {
component.editThis("5c7d5fde213e25232864dbe0");
expect(new MockRouter().navigate).toHaveBeenCalledWith(['/categories/edit'], { queryParams: { id: "5c7d5fde213e25232864dbe0" } });
});

});

最佳答案

更改您的线路:

expect(new MockRouter().navigate).toHaveBeenCalledWith(['/categories/edit'], { queryParams: { id: "5c7d5fde213e25232864dbe0" } });

到以下几点:
expect(TestBed.get(Router).navigate).toHaveBeenCalledWith(['/categories/edit'], { queryParams: { id: "5c7d5fde213e25232864dbe0" } });

这将获得在 TestBed 中实例化的实际路由器对象。

我希望这有帮助。

关于angular - 如何使用 Angular 中的查询参数对 'navigate' 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55042116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com