Javascript 错误TS2304:找不到名称';createNewEvent';
我正在为Angular 11中的输入框编写一个单元测试 这是我的spec.ts代码:Javascript 错误TS2304:找不到名称';createNewEvent';,javascript,angular,unit-testing,jasmine,integration-testing,Javascript,Angular,Unit Testing,Jasmine,Integration Testing,我正在为Angular 11中的输入框编写一个单元测试 这是我的spec.ts代码: import { ComponentFixture, TestBed } from '@angular/core/testing'; import 'jasmine'; import { ExpectedConditions } from 'protractor'; import { MicroplateComponent } from './microplate.component'; describe('
import { ComponentFixture, TestBed } from '@angular/core/testing';
import 'jasmine';
import { ExpectedConditions } from 'protractor';
import { MicroplateComponent } from './microplate.component';
describe('MicroplateComponent', () => {
let component: MicroplateComponent;
let fixture: ComponentFixture<MicroplateComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [MicroplateComponent]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(MicroplateComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
describe('Evaluation of input box', () => {
it('Should return true for "1, 2, 3"', () => {
const input = fixture.nativeElement.querySelector('input');
const event = createNewEvent('input'); // I get error at this line
input.value = '1, 2, 3';
input.dispatchEvent(event);
fixture.detectChanges();
expect(component.columns?.valid).toBe(true);
});
});
});
从'@angular/core/testing'导入{ComponentFixture,TestBed};
进口‘茉莉花’;
从“量角器”导入{ExpectedConditions};
从“./microplate.component”导入{MicroplateComponent};
描述('MicroplateComponent',()=>{
let组分:微板组分;
let夹具:组件夹具;
beforeach(异步()=>{
等待TestBed.configureTestingModule({
声明:[微板组件]
})
.compileComponents();
});
在每个之前(()=>{
fixture=TestBed.createComponent(MicroplateComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
描述('输入框评估',()=>{
它('应该为“1,2,3”返回true,()=>{
常量输入=fixture.nativeElement.querySelector('input');
const event=createNewEvent('input');//我在这一行得到错误
input.value='1,2,3';
输入、调度事件(事件);
fixture.detectChanges();
expect(component.columns?.valid).toBe(true);
});
});
});
我得到以下错误,但我不知道我错在哪里
错误TS2304:找不到名称“createNewEvent”。
我曾在angular网站上使用过这种方法。如果您试图调用输入事件,您就是这样做的:
input.dispatchEvent(new Event('input'));
如果您正试图调用输入事件,请执行以下操作:
input.dispatchEvent(new Event('input'));
缺少导入<未定义代码>createNewEventanywhere@MikeS.,因为我不知道“createNewEvent”位于何处。我想它是component.createNewEvent或其他什么。。。(假设它是组件中的函数)缺少导入<未定义代码>createNewEventanywhere@MikeS.,因为我不知道“createNewEvent”位于何处。我想它是component.createNewEvent或其他什么。。。(假设它是组件中的一个函数)