繁体   English   中英

如何在angular2中使用谓词

[英]How to use predicate in angular2

我使用angular2创建了一个登录页面。 它有两个输入字段作为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

});

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

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

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

const button = fixture.debugElement.query(By.css('.the-button')).nativeElement;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM