如何在angular2中使用谓词

hen*_*ick 9 testing typescript karma-jasmine angular

我已经创建了一个登录页面angular2.它有两个输入字段as userNamepassword.登录中有一个按钮.当我打算为此编写测试用例时loginComponent,不得不predicate用来识别button.这是我的测试用例.

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { LoginComponent } from './login.component';


export class LoginComponentTest {
  constructor(public loginComponent: LoginComponent){
    this.loginComponent.logIn('Chathura', '1234');

  }
}

describe('LoginComponent', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        LoginComponent
      ],
    })
  });

  const fixture = TestBed.createComponent(LoginComponent);

  const button = fixture.query(predicate: Predicate<DebugElement>, scope?: Function) : DebugElement

});
Run Code Online (Sandbox Code Playgroud)

我将通过提供输入值进行测试,然后单击登录按钮,看看接下来会发生什么.

我无法找到正确使用谓词的正确教程.

Pau*_*tha 3

创建 a 的最简单方法Predicate是使用By静态方法之一,即css, directive

const button = fixture.debugElement.query(By.css('.the-button')).nativeElement;
Run Code Online (Sandbox Code Playgroud)

By.css你传递任何 css 选择器。的结果query将是 a ,因此如果您想与本机 DOM 元素交互,则DebugElement需要获取。nativeElement