Angular 如何在订阅中测试setter?有棱角的
我有以下代码:Angular 如何在订阅中测试setter?有棱角的,angular,testing,jasmine,setter,subscribe,Angular,Testing,Jasmine,Setter,Subscribe,我有以下代码: public ngOnInit(): void { this.searchControl.valueChanges.pipe(debounceTime(300), this.takeUntilDestroyed()).subscribe(query => { this.officeService.searchQuery = query; }); } 如何使用覆盖率搜索查询设置器 我写了测试谁通过了,但没有覆盖率设置器 我的测试: i
public ngOnInit(): void {
this.searchControl.valueChanges.pipe(debounceTime(300), this.takeUntilDestroyed()).subscribe(query => {
this.officeService.searchQuery = query;
});
}
如何使用覆盖率搜索查询设置器
我写了测试谁通过了,但没有覆盖率设置器
我的测试:
it('should searchQuery called ', () => {
const { component, service } = setup();
const query: string = 'test';
const fixture: ComponentFixture<SearchComponent> = TestBed.createComponent(SearchComponent);
const input = fixture.debugElement.query(By.css('input'));
const inputEvent = new Event('input');
const spiez: jasmine.Spy = spyOnProperty(service, 'searchQuery', 'set');
component.searchControl.valueChanges.subscribe((tmp: string) => expect(spiez).toHaveBeenCalledWith(tmp));
input.nativeElement.value = 'newString';
input.nativeElement.dispatchEvent(inputEvent);
fixture.detectChanges();
});
it('should searchQuery called',()=>{
const{component,service}=setup();
常量查询:字符串='test';
const fixture:ComponentFixture=TestBed.createComponent(SearchComponent);
const input=fixture.debugElement.query(By.css('input'));
const inputEvent=新事件(“输入”);
const-spiez:jasmine.Spy=spyOnProperty(服务,'searchQuery','set');
subscribe((tmp:string)=>expect(spiez).toHaveBeenCalledWith(tmp));
input.nativeElement.value='newString';
input.nativeElement.dispatchEvent(inputEvent);
fixture.detectChanges();
});
我的测试使用了fakeAsync和勾选:
const { component, service } = setup();
const query: string = 'test';
const fixture: ComponentFixture<SearchComponent> = TestBed.createComponent(SearchComponent);
const input = fixture.debugElement.query(By.css('input'));
const inputEvent = new Event('input');
const spiez: jasmine.Spy = spyOnProperty(service, 'searchQuery', 'set');
component.searchControl.valueChanges.subscribe((tmp: string) => expect(spiez).toHaveBeenCalledWith(tmp));
input.nativeElement.value = 'newString';
input.nativeElement.dispatchEvent(inputEvent);
fixture.detectChanges();
tick(300);
}));
const{component,service}=setup();
常量查询:字符串='test';
const fixture:ComponentFixture=TestBed.createComponent(SearchComponent);
const input=fixture.debugElement.query(By.css('input'));
const inputEvent=新事件(“输入”);
const-spiez:jasmine.Spy=spyOnProperty(服务,'searchQuery','set');
subscribe((tmp:string)=>expect(spiez).toHaveBeenCalledWith(tmp));
input.nativeElement.value='newString';
input.nativeElement.dispatchEvent(inputEvent);
fixture.detectChanges();
勾选(300);
}));
让我们试试这个:
describe('SearchComponent', () => {
let component: SearchComponent;
let fixture: ComponentFixture<SearchComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SearchComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should test value changes', () => {
const testValue = 'test';
const input = fixture.debugElement.query(By.css('input'));
const spiez: jasmine.Spy = spyOnProperty(service, 'searchQuery');
tick();
fixture.detectChanges()
input.nativeElement.value = testValue;
dispatchEvent(input.nativeElement, "input");
tick();
fixture.detectChanges();
expect(spiez).toHaveBeenCalledWith(testValue))
}));
description('SearchComponent',()=>{
let组件:SearchComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[搜索组件]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(SearchComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应测试值变化',()=>{
常量testValue='test';
const input=fixture.debugElement.query(By.css('input'));
const-spiez:jasmine.Spy=spyOnProperty(服务,'searchQuery');
勾选();
fixture.detectChanges()
input.nativeElement.value=testValue;
dispatchEvent(input.nativeElement,“输入”);
勾选();
fixture.detectChanges();
期望(spiez).已被调用(testValue))
}));
通过修改fakeAsync测试中搜索控件的值并勾选300ms:@JBNizet check edit。像这样吗?否。在测试中订阅是无用的。您已经在组件中订阅了,并且该订阅应该在300ms后设置属性。因此,您的属性设置检查应该在调用勾选之后进行(300).我有一个错误:error:应使用:['test'调用spy searchQuery]但它从未被调用。
我在那里更新了muy asnwer。请,现在试试看它是否有效。同样的错误。我尝试了所有方法,但我不知道:DDo别担心,我们将继续尝试,直到测试有效!!!我刚刚更新了我的答案,现在就尝试