gpt4 book ai didi

Angular Testing - ngBootstraps typeahead

转载 作者:太空狗 更新时间:2023-10-29 17:20:10 27 4
gpt4 key购买 nike

我目前正在使用 ngBootstrap 的自动完成机制(提前输入)。现在我想对输入事件的每个序列是否调用一个方法进行单元测试。我的测试用例目前的错误是:Cannot read property 'pipe' of undefined

HTML:

<input id="locationEdit" type="text" class="form-control"
[(ngModel)]="user.location" name="location [ngbTypeahead]="search"/>

组件:

public ngOnInit() {
this.search = (text$: Observable<string>) =>
text$.pipe(
tap(() => {
this.isSearching = true;
this.searchFailed = false;
}),
debounceTime(750),
distinctUntilChanged(),
switchMap(term =>
this.cityService.getLocation(term).pipe(
tap((response) => {
this.searchFailed = response.length === 0;
this.isSearching = false;
})))
);
}

规范.ts

  it('should call spy on city search', fakeAsync(() => {
component.user = <User>{uid: 'test', username: 'mleko', location: null, description: null};
const spy = (<jasmine.Spy>cityStub.getLocation).and.returnValue(of['München Bayern']);

fixture.detectChanges();
const compiled: DebugElement = fixture.debugElement.query(By.css('#locationEdit'));
compiled.nativeElement.value = 'München';
compiled.nativeElement.dispatchEvent(new Event('input'));

tick(1000);
fixture.detectChanges();

expect(spy).toHaveBeenCalled();
}));

有人可以帮我正确模拟 this.search 吗?

编辑

根据@dmcgrandle 的绝妙建议,我不需要渲染 HTML 和模拟输入事件来检查预输入是否正常工作。我宁愿做一个 Observable,它发出值并将其分配给函数。一种方法是:

  it('should call spy on city search', fakeAsync(() => {
const spy = (<jasmine.Spy>cityStub.getLocation).and.returnValue(of['München Bayern']);

component.ngOnInit();
const textMock = of(['M', 'Mün', 'München']).pipe(flatMap(index => index));

component.search(textMock);

tick();

expect(spy).toHaveBeenCalled();
}));

但问题仍然是,component.search 没有调用 spy 。在 switchMap 运算符的搜索函数中,我添加了一个 console.log 以查看函数是否发出了值。但事实并非如此。

最佳答案

我不认为你真的想在测试期间调用任何 ngBootstrap 代码——毕竟你想对你的代码进行单元测试,而不是他们的。 :)

因此,我建议通过设置您自己的定时 Observable 并使用它调用您的函数来模拟用户实际键入的内容。也许每 100 毫秒模拟发送一个字符。像这样:

it('should call spy on city search', fakeAsync(() => {
component.user = <User>{uid: 'test', username: 'mleko', location: null, description: null};
// Change next line depending on implementation of cityStub ...
const spy = spyOn(cityStub, 'getLocation').and.returnValue(of('München Bayern'));

fixture.detectChanges();
let inputTextArray = ['M', 'Mü', 'Mün', 'Münc', 'Münch', 'Münche', 'München'];
let textMock$ : Observable<string> = interval(100).pipe(take(7),map(index => inputTextArray[index]));
component.search(textMock$);
tick(1000);
expect(spy).toHaveBeenCalled();
}));

更新:

我在这里整理了一个 stackblitz 来测试它:https://stackblitz.com/edit/stackoverflow-question-52914753 (打开左侧的 app 文件夹,然后单击 my.component.spec.ts 以查看测试文件)

一旦我把它放在那里,就很明显出了什么问题——没有订阅可观察对象,因为订阅似乎是由 ngBootstrap 完成的,所以为了测试我们需要明确订阅。这是我建议的新规范(取自 stackblitz):

it('should call spy on city search', fakeAsync(() => {
const cityStub = TestBed.get(CityService);
const spy = spyOn(cityStub, 'getLocation').and.returnValue(of('München Bayern'));

fixture.detectChanges();
let inputTextArray = ['M', 'Mü', 'Mün', 'Münc', 'Münch', 'Münche', 'München'];
let textMock$ : Observable<string> = interval(100).pipe(take(7),map(index => inputTextArray[index]));
component.search(textMock$).subscribe(result => {
expect(result).toEqual('München Bayern');
});
tick(1000);
expect(spy).toHaveBeenCalled();
}));

关于 Angular Testing - ngBootstraps typeahead,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52914753/

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