【问题标题】:Angular testing and ngModelAngular 测试和 ngModel
【发布时间】:2018-01-11 10:50:13
【问题描述】:

我正在使用 Angular 编写我的第一个组件测试,但在使 ngModel 绑定工作时遇到了一些困难。 这是我的测试模块定义:

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        LdapLoginComponent,
      ],
      imports: [
        CommonModule,
        FormsModule,
        NoopAnimationsModule,
        MatInputModule,
        MatFormFieldModule,
        RouterTestingModule,
      ],
      providers: [
        {
          provide: AuthorizationService,
          useValue: { login() {} },
        },
      ]
    }).compileComponents();
  }));

这里是我的测试用例:

it('should bind form fields with class', fakeAsync(() => {
    // Given
    const username = 'username';
    const password = 'password';
    const usernameField = de.query(By.css('input[name=username]')).nativeElement;
    const passwordField = de.query(By.css('input[name=password]')).nativeElement;

    // When
    usernameField.value = username;
    passwordField.value = password;
    usernameField.dispatchEvent(new Event('input'));
    passwordField.dispatchEvent(new Event('input'));
    tick();
    fixture.detectChanges();

    // Then
    expect(comp.username).toEqual(username);
    expect(comp.password).toEqual(password);
  }));

我的组件类:

export class LdapLoginComponent {

  username: string;
  password: string;
  errorMessage: string;
  submitDisabled = false;

  constructor(
    private authorizationService: AuthorizationService,
    private router: Router,
  ) {
  }

  login(): void {
    delete this.errorMessage;
    this.submitDisabled = true;
    this.authorizationService.login(AuthorizationProvider.LDAP, this.username, this.password)
      .subscribe(
        () => {
          this.router.navigate(['/']);
        },
        (err: Error) => {
          this.errorMessage = err.message;
          this.submitDisabled = false;
        },
      );
  }

}

还有我的组件模板:

<form class="form-container" (submit)="login()">
  <mat-form-field color="warn">
    <input
    matInput
    type="text"
    name="username"
    placeholder="Insert your username"
    [(ngModel)]="username"
    required
    i18n-placeholder="@@input.placeholder.username">
  </mat-form-field>
  <mat-form-field color="warn">
    <input
      matInput
      type="password"
      name="password"
      placeholder="Insert your password"
      [(ngModel)]="password"
      required
      i18n-placeholder="@@input.placeholder.password">
  </mat-form-field>
  <button
    mat-raised-button
    type="submit"
    color="warn"
    [disabled]="submitDisabled"
    i18n="@@input.submit">Submit</button>
</form>
<article>{{errorMessage}}</article>

我正在更改我的测试中的用户名和密码字段的值,并且我希望我的班级的用户名和密码字段能够相应地更新。如果我在浏览器中手动测试一切正常,但不是在测试中。

有什么想法吗?

谢谢。

【问题讨论】:

    标签: angular testing ngmodel


    【解决方案1】:

    看起来对于 mat-input 字段,我们需要在更改输入之前做一个 focus():

    it('should bind form fields with class', fakeAsync(() => {
        // Given
        const username = 'username';
        const password = 'password';
        const usernameField = de.query(By.css('input[name=username]')).nativeElement;
        const passwordField = de.query(By.css('input[name=password]')).nativeElement;
    
        usernameField.focus();
        passwordField.focus();
    
        // When
        usernameField.value = username;
        passwordField.value = password;
        usernameField.dispatchEvent(new Event('input'));
        passwordField.dispatchEvent(new Event('input'));
        tick();
        fixture.detectChanges();
    
        // Then
        expect(comp.username).toEqual(username);
        expect(comp.password).toEqual(password);
    }));
    

    【讨论】:

      【解决方案2】:

      问题是在调用 dispatchEvent 之前您实际上并没有设置输入字段的值。您正在直接设置组件属性。

      comp.username = username;
      usernameField.dispatchEvent(new Event('input'));
      

      应该是

      let usernameFieldElement = usernameField.nativeElement;
      usernameFieldElement.value = username;
      usernameField.dispatchEvent(new Event('input'));
      

      密码也一样。

      另一件事是您同时测试三件事,将文本输入到输入区域,单击按钮运行登录和登录功能本身。我建议将它们分成三个断言。

      1. 如上设置字段并检查数据绑定。

      2. 单击按钮并监视登录函数以检查它是否已被调用。

      3. 设置实际组件属性,直接调用logon(),检查你的navigationSpy是否被正确调用。

      这样,如果出现问题,您将能够更容易地找到它。

      【讨论】:

      • 代码中有错字,你是对的。是的,我完全同意你的观点,我应该拆分那个测试。我用新代码更新了我的帖子,但我仍然遇到同样的绑定问题:-(
      猜你喜欢
      • 1970-01-01
      • 2018-03-18
      • 2017-08-28
      • 2017-03-07
      • 1970-01-01
      • 2017-08-17
      • 2018-07-24
      • 2018-02-17
      • 1970-01-01
      相关资源
      最近更新 更多