您好,我正在使用 Angular5 Jasmine 编写单元测试用例。我正在尝试提交表格。我正在编写提交表单的单元测试用例。下面是我的表格。
<form *ngIf="formResetToggle" class="form-horizontal" name="scopesEditorForm" #f="ngForm" novalidate (ngSubmit)="f.form.valid && isScopeValid() ? saveScope() : showErrorAlert('Please enter mandatory fields')">
<input autofocus type="text" id="scopename" name="scopename" placeholder="Enter scope name" class="form-control" [(ngModel)]="scopeEdit.scopevalue" #scopename="ngModel" required />
<button type="button" (click)="editorModal.hide()" class="btn btn-default" data-dismiss="modal">Cancel</button>
</form>
下面是我的 spec.ts 文件。
describe('ScopeEditorComponent', () => {
let component: ScopeEditorComponent;
let fixture: ComponentFixture<ScopeEditorComponent>;
let submitEl: DebugElement;
let scopeValue: DebugElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientModule,
RouterTestingModule,
],
declarations: [
ScopeEditorComponent,
SearchBoxComponent,
GroupByPipe,
]
fixture = TestBed.createComponent(ScopeEditorComponent);
component = fixture.componentInstance;
service = new PermissionEndpointMock();
fixture.detectChanges();
submitEl = fixture.debugElement.query(By.css('button')).nativeElement;
scopeValue = fixture.debugElement.query(By.css('#scopename'));
}));
it('should create the scope component', async(() => {
expect(component).toBeTruthy();
}));
it('add scope', () => {
let scope: Scope;
scopeValue.nativeElement.value = "test@example.com";
// Subscribe to the Observable and store the user in a local variable.
component.addscopeEventEmitter.subscribe((value) => scope = value);
// This sync emits the event and the subscribe callback gets executed above
submitEl.triggerEventHandler('click', null);
// Now we can check to make sure the emitted value is correct
expect(scope.scopeid).toBe("123456");
});
以下代码抛出错误 submitEl.triggerEventHandler 不是函数。有人可以帮我解决这个问题吗?谢谢
最佳答案
尝试使用
submitEl = fixture.debugElement.nativeElement.querySelector('button');
然后
submitEl.click();
关于angular - submitEl.triggerEventHandler 不是 Angular 5 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50330523/