我正在测试的方法如下:

/**
   * Update properties when the applicant changes the payment term value.
   * @return {Mixed} - Either an Array where the first index is a boolean indicating
   *    that selectedPaymentTerm was set, and the second index indicates whether
   *    displayProductValues was called. Or a plain boolean indicating that there was an
   *    error.
   */
  onPaymentTermChange() {
    this.paymentTerm.valueChanges.subscribe(
      (value) => {
        this.selectedPaymentTerm = value;
        let returnValue = [];
        returnValue.push(true);
        if (this.paymentFrequencyAndRebate) {
          returnValue.push(true);
          this.displayProductValues();
        } else {
          returnValue.push(false);
        }
        return returnValue;
      },
      (error) => {
        console.warn(error);
        return false;
      }
    )
  }

如您所见,paymentTerm是一个返回Observable的窗体控件,然后将其订阅并检查了返回值。

我似乎找不到有关单元测试FormControl的任何文档。我最接近的是有关模拟Http请求的文章,这是一个类似的概念,因为它们正在返回Observables,但我认为它并不完全适用。

作为引用,我使用的是Angular RC5,使用Karma运行测试,并且框架为Jasmine。

最佳答案

更新

至于这个关于异步行为的答案的第一部分,我发现您可以使用fixture.whenStable()来等待异步任务。因此,无需仅使用内联模板

it('', async(() => {
  fixture.whenStable().then(() => {
    // your expectations.
  })
})

首先,让我们解决测试组件中异步任务的一些常规问题。在测试不受测试控制的异步代码时,应使用fakeAsync,因为它将允许我们调用tick(),这会使操作在测试时显得同步。例如

class ExampleComponent implements OnInit {
  value;

  ngOnInit() {
    this._service.subscribe(value => {
      this.value = value;
    });
  }
}

it('..', () => {
  const fixture = TestBed.createComponent(ExampleComponent);
  fixture.detectChanges();
  expect(fixture.componentInstance.value).toEqual('some value');
});

由于调用了ngOnInit,该测试将失败,但是Observable是异步的,因此该值不会为测试中的syncnus调用及时设置(即expect)。

为了解决这个问题,我们可以使用fakeAsynctick强制测试等待所有当前异步任务完成,从而使其在测试中看起来像是在同步。

import { fakeAsync, tick } from '@angular/core/testing';

it('..', fakeAsync(() => {
  const fixture = TestBed.createComponent(ExampleComponent);
  fixture.detectChanges();
  tick();
  expect(fixture.componentInstance.value).toEqual('some value');
}));

现在,假设Observable订阅中没有意外的延迟,那么测试应该通过了,在这种情况下,我们甚至可以在滴答调用tick(1000)中传递毫秒的延迟。

这个(fakeAsync)是有用的功能,但是问题是,当我们在templateUrl中使用@Component时,它会进行XHR调用,并进行XHR calls can't be made in a fakeAsync 。如this post所述,在某些情况下您可以模拟服务以使其同步,但是在某些情况下,这是不可行或太困难的。对于表格,这是不可行的。

因此,在处理表单时,我倾向于将模板放在template中,而不是放在外面的templateUrl中,并且将表单分成较小的组件(如果它们确实很大)(只是在组件文件中没有很大的字符串)。我能想到的唯一其他选择是在测试中使用setTimeout,以使异步操作通过。这是一个偏好问题。我刚决定在处理表单时使用内联模板。它破坏了我的应用程序结构的一致性,但是我不喜欢setTimeout解决方案。

现在,就形式的实际测试而言,我发现的最佳来源只是看source code integration tests。您需要将标签更改为所使用的Angular版本,因为默认的master分支可能与所使用的版本不同。

以下是一些示例。

测试输入时,请更改nativeElement上的输入值,然后使用input调度dispatchEvent事件。例如

@Component({
  template: `
    <input type="text" [formControl]="control"/>
  `
})
class FormControlComponent {
  control: FormControl;
}

it('should update the control with new input', () => {
  const fixture = TestBed.createComponent(FormControlComponent);
  const control = new FormControl('old value');
  fixture.componentInstance.control = control;
  fixture.detectChanges();

  const input = fixture.debugElement.query(By.css('input'));
  expect(input.nativeElement.value).toEqual('old value');

  input.nativeElement.value = 'updated value';
  dispatchEvent(input.nativeElement, 'input');

  expect(control.value).toEqual('updated value');
});

这是从源集成测试中提取的一个简单测试。下面有更多的测试示例,另外一个是从源头获取的,还有两个没有的示例,只是为了展示其他不在测试中的方式。

对于您的特定情况,看起来您正在使用(ngModelChange),在其中您将 call 分配给onPaymentTermChange()。在这种情况下,您的实现就没有多大意义了。当值更改时,(ngModelChange)已经会吐出一些东西,但是每次模型更改时您都在订阅。您应该做的是接受$event参数,由change事件发出

(ngModelChange)="onPaymentTermChange($event)"

每次更改时,您都会获得新值的传递。因此,只需在您的方法中使用该值即可,而不是订阅。 $event将是新值。

如果您确实想在valueChange上使用FormControl,则应该改为在ngOnInit中开始收听,因此您只需订阅一次。您将在下面看到一个示例。我个人不会走这条路线。我只是按照您的方式进行操作,但是除了订阅更改之外,还可以接受更改中的事件值(如前所述)。

这是一些完整的测试

import {
  Component, Directive, EventEmitter,
  Input, Output, forwardRef, OnInit, OnDestroy
} from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser/src/dom/debug/by';
import { getDOM } from '@angular/platform-browser/src/dom/dom_adapter';
import { dispatchEvent } from '@angular/platform-browser/testing/browser_util';
import { FormControl, ReactiveFormsModule } from '@angular/forms';

class ConsoleSpy {
  log = jasmine.createSpy('log');
}

describe('reactive forms: FormControl', () => {
  let consoleSpy;
  let originalConsole;

  beforeEach(() => {
    consoleSpy = new ConsoleSpy();
    originalConsole = window.console;
    (<any>window).console = consoleSpy;

    TestBed.configureTestingModule({
      imports: [ ReactiveFormsModule ],
      declarations: [
        FormControlComponent,
        FormControlNgModelTwoWay,
        FormControlNgModelOnChange,
        FormControlValueChanges
      ]
    });
  });

  afterEach(() => {
    (<any>window).console = originalConsole;
  });

  it('should update the control with new input', () => {
    const fixture = TestBed.createComponent(FormControlComponent);
    const control = new FormControl('old value');
    fixture.componentInstance.control = control;
    fixture.detectChanges();

    const input = fixture.debugElement.query(By.css('input'));
    expect(input.nativeElement.value).toEqual('old value');

    input.nativeElement.value = 'updated value';
    dispatchEvent(input.nativeElement, 'input');

    expect(control.value).toEqual('updated value');
  });

  it('it should update with ngModel two-way', fakeAsync(() => {
    const fixture = TestBed.createComponent(FormControlNgModelTwoWay);
    const control = new FormControl('');
    fixture.componentInstance.control = control;
    fixture.componentInstance.login = 'old value';
    fixture.detectChanges();
    tick();

    const input = fixture.debugElement.query(By.css('input')).nativeElement;
    expect(input.value).toEqual('old value');

    input.value = 'updated value';
    dispatchEvent(input, 'input');
    tick();

    expect(fixture.componentInstance.login).toEqual('updated value');
  }));

  it('it should update with ngModel on-change', fakeAsync(() => {
    const fixture = TestBed.createComponent(FormControlNgModelOnChange);
    const control = new FormControl('');
    fixture.componentInstance.control = control;
    fixture.componentInstance.login = 'old value';
    fixture.detectChanges();
    tick();

    const input = fixture.debugElement.query(By.css('input')).nativeElement;
    expect(input.value).toEqual('old value');

    input.value = 'updated value';
    dispatchEvent(input, 'input');
    tick();

    expect(fixture.componentInstance.login).toEqual('updated value');
    expect(consoleSpy.log).toHaveBeenCalledWith('updated value');
  }));

  it('it should update with valueChanges', fakeAsync(() => {
    const fixture = TestBed.createComponent(FormControlValueChanges);
    fixture.detectChanges();
    tick();

    const input = fixture.debugElement.query(By.css('input')).nativeElement;

    input.value = 'updated value';
    dispatchEvent(input, 'input');
    tick();

    expect(fixture.componentInstance.control.value).toEqual('updated value');
    expect(consoleSpy.log).toHaveBeenCalledWith('updated value');
  }));
});

@Component({
  template: `
    <input type="text" [formControl]="control"/>
  `
})
class FormControlComponent {
  control: FormControl;
}

@Component({
  selector: 'form-control-ng-model',
  template: `
    <input type="text" [formControl]="control" [(ngModel)]="login">
  `
})
class FormControlNgModelTwoWay {
  control: FormControl;
  login: string;
}

@Component({
  template: `
    <input type="text"
           [formControl]="control"
           [ngModel]="login"
           (ngModelChange)="onModelChange($event)">
  `
})
class FormControlNgModelOnChange {
  control: FormControl;
  login: string;

  onModelChange(event) {
    this.login = event;
    this._doOtherStuff(event);
  }

  private _doOtherStuff(value) {
    console.log(value);
  }
}

@Component({
  template: `
    <input type="text" [formControl]="control">
  `
})
class FormControlValueChanges implements OnDestroy {
  control: FormControl;
  sub: Subscription;

  constructor() {
    this.control = new FormControl('');
    this.sub = this.control.valueChanges.subscribe(value => {
      this._doOtherStuff(value);
    });
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }

  private _doOtherStuff(value) {
    console.log(value);
  }
}

关于unit-testing - 如何在Angular2中对FormControl进行单元测试,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39481324/

10-12 00:38
查看更多