angular - submitEl.triggerEventHandler 不是 Angular 5 中的函数

标签 angular typescript unit-testing jasmine

您好,我正在使用 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/

相关文章:

angular2 MdDialog 没有显示为弹出窗口

typescript - 如何在 typescript 的接口(interface)或对象文字类型中使某些属性成为必需但全部允许?

unit-testing - 比较 Rust 中的相等性函数

ruby-on-rails - 未初始化的常量 Test::Unit::TestResult::TestResultFailureSupport

angular - 具有动态 url 的 routerLinkActive 不起作用 Angular 9

angular - 运行 start :prod on fresh project 时,由 node_modules 中的 @types 依赖项引起的 NestJs 错误

angular - RXJS 扩展从不退出递归调用

angular - ionic2:无法从弹出窗口导航到另一个页面

c# - 除非删除 App.Config,否则不会发现 Visual Studio 2017 单元测试

angular - Ionic 3、Angular 4 中的单例