小编Gau*_*rav的帖子

如何对 ngControl 装饰有 @Self 的反应式组件进行单元测试

我通过注入 NgControl 编写了一个反应式组件,并使用 @Self 装饰器进行装饰。我的问题与此类组件的单元测试有关。请看下面的代码:

免责声明:我已经快速复制了代码并进行了一些内联更改。因此,这可能不是编译器满意的代码。

我的反应组件:

@Component({
  selector: 'text-input',
  templateUrl: '<input type="text" class="native_input" />'
})
class TextInput implements ControlValueAccessor {
  protected constructor(@Self() public controlDir: NgControl) {

    this.controlDir.valueAccessor = this;
  }

  // ...followed by other ControlValueAccessor methods

}
Run Code Online (Sandbox Code Playgroud)

单元测试:

describe('TextInput -', () => {

   let fixture: ComponentFixture<TextInputHost>;
   let textInput: TextInput;

   let inputElement: HTMLInputElement;

   beforeEach(async(() => {
     TestBed.configureTestingModule({
       declarations: [
         TextInput, TextInputHost
       ],
       imports: [
         FormsModule, ReactiveFormsModule
       ]
     });
  }));

  beforeEach(fakeAsync(() => {
    fixture = getTestBed().createComponent(TextInputHost);
    textInput = fixture.componentInstance.textInputComponent;
    textInput.writeValue('TestValue');
    inputElement …
Run Code Online (Sandbox Code Playgroud)

unit-testing angular-components angular angular-reactive-forms

6
推荐指数
1
解决办法
1886
查看次数