Boo*_*onZ 8 javascript unit-testing karma-jasmine angular
我在测试 ActivatedRoute queryParams 订阅中的逻辑时遇到问题。
constructor(private router: Router, private route: ActivatedRoute, private auth: AuthService) {}
ngOnInit(): void {
this.route.queryParams.subscribe((params:any) => {
if(params['data']) {
this.handle(params['data']);
} else {
if (this.auth.isAuthenticated()) {
this.router.navigate(['/home']);
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
我想测试:
this.handle()模拟时触发params['data']this.auth.isAuthenticated()回报true是this.router.navigate被称为我已经尝试了很多东西,但我的想法已经用完了。
我的测试文件:
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
const mockService = {
navigate: jasmine.createSpy('navigate'),
isAuthenticated: jasmine.createSpy('isAuthenticated'),
queryParams: jasmine.createSpy('queryParams')
};
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
providers: [
{ provide: Router, useValue: mockService },
{ provide: ActivatedRoute, useValue: mockService },
{ provide: AuthService, useValue: mockService }
]
}).compileComponents();
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
mockService.navigate.calls.reset();
}));
it('should create the test component', () => {
expect(component).toBeTruthy();
});
it('should navigate away when authenticated', () => {
mockService.isAuthenticated.and.returnValue(true);
mockService.queryParams.and.callFake((data, params) => new Observable(o => o.next({ params: {} })));
component.ngOnInit();
expect(mockService.navigate).toHaveBeenCalledWith(['/home']);
});
});
Run Code Online (Sandbox Code Playgroud)
但是我得到了TypeError: this.route.queryParams.subscribe is not a function。我知道这mockService.isAuthenticated.and.returnValue(true);工作正常,因为在使用订阅参数之前,我只有这个 if 语句ngOnInit()。
我试图将 mockService 更改为:
const mockService = {
navigate: jasmine.createSpy('navigate'),
isAuthenticated: jasmine.createSpy('isAuthenticated'),
queryParams: {
subscribe: jasmine.create('subscribe')
}
};
Run Code Online (Sandbox Code Playgroud)
我也试过:
const mockService = {
navigate: jasmine.createSpy('navigate'),
isAuthenticated: jasmine.createSpy('isAuthenticated'),
queryParams: {
subscribe: Observable.of({ params: {} })
}
};
Run Code Online (Sandbox Code Playgroud)
但没有成功,最后两个我得到 Expected spy navigate to have been called with [ [ '/home' ] ] but it was never called.
那么有人知道如何正确测试 querParams 订阅中的逻辑吗?
Raf*_*lin 11
我不能说现在回答这个问题是否为时已晚,但也许它会帮助新的谷歌员工......
我设法通过这种方式解决了这个问题:
class ActivatedRouteMock {
queryParams = new Observable(observer => {
const urlParams = {
param1: 'some',
param2: 'params'
}
observer.next(urlParams);
observer.complete();
});
}
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
HttpClient,
HttpHandler,
ControlContainer,
{
provide: ActivatedRoute,
useClass: ActivatedRouteMock
}
]
}).compileComponents();
injector = getTestBed();
httpMock = injector.get(HttpTestingController);
}));Run Code Online (Sandbox Code Playgroud)
这允许您断言.subscribe(() => {})方法中的逻辑..
希望它有帮助
您可以在 useValue 中使用 rxjs observable 来模拟它。
const router = {
navigate: jasmine.createSpy('navigate')
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([]),
RouterTestingModule,
PlanPrimengModule
],
declarations: [YourComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [
provideMockStore(),
{
provide: ActivatedRoute,
useValue: {
queryParams: of({
param1: "value1",
param1: "value2"
})
}
},
{ provide: Router, useValue: router }
]
}).compileComponents(); }));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6043 次 |
| 最近记录: |