Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何测试angular 2指令提供的参数与调用目标的任何签名都不匹配_Angular_Testing_Directive - Fatal编程技术网

如何测试angular 2指令提供的参数与调用目标的任何签名都不匹配

如何测试angular 2指令提供的参数与调用目标的任何签名都不匹配,angular,testing,directive,Angular,Testing,Directive,我有我要测试的指令。每次运行测试时,我都会收到一个错误,该错误表示“提供的参数与调用目标的任何签名都不匹配”。如何将构造函数的参数引入测试 这是我的指示: import { Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core'; @Directive({ selector: '[fbInputlistener]' }) export class InputlistenerDirective {

我有我要测试的指令。每次运行测试时,我都会收到一个错误,该错误表示“提供的参数与调用目标的任何签名都不匹配”。如何将构造函数的参数引入测试

这是我的指示:

import { Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core';

@Directive({
  selector: '[fbInputlistener]'
})
export class InputlistenerDirective {

  constructor( private elRef: ElementRef, private renderer: Renderer ) { }

  @HostListener('keyup') onKeyUp() {
    if(this.elRef.nativeElement.value.length > 0) {
      this.renderer.setElementClass(this.elRef.nativeElement, 'form__input--has-value', true);
    } else {
      this.renderer.setElementClass(this.elRef.nativeElement, 'form__input--has-value', false);
     }
  }
}
这是它的测试:

import { InputlistenerDirective } from './inputlistener.directive';

describe('InputlistenerDirective', () => {
  it('should create an instance', () => {
    const directive = new InputlistenerDirective();
    expect(directive).toBeTruthy();
  });
});
从'@angular/core/testing'导入{TestBed,ComponentFixture};
从“@angular/platform browser”导入{By}”;
从“@angular/core”导入{Component,DebugElement};
从“./inputlistener.directive”导入{InputlistenerDirective};
@组成部分({
模板:``
})
类TestInputComponent{
}
描述('InputlistenerDirective',()=>{
let组件:TestInputComponent;
let夹具:组件夹具;
let inputEl:DebugElement;
在每个之前(()=>{
TestBed.configureTestingModule({
声明:[TestInputComponent,InputlistenerDirective]
});
fixture=TestBed.createComponent(TestInputComponent);
组件=fixture.componentInstance;
inputEl=fixture.debugElement.query(By.css('input'));
});
它('指令应将类添加到测试输入',()=>{
inputEl.triggerEventHandler('keyup',null);
fixture.detectChanges();
expect(inputEl.nativeElement.classList.length).toBe(1);
});
});

我只想在一个虚拟模拟组件上托管该指令,并使用测试床使用完整的框架对其进行测试。@peeskillet-Yup完成了这项工作。非常感谢你。
import {TestBed, ComponentFixture} from '@angular/core/testing';
import {By} from "@angular/platform-browser";
import {Component, DebugElement} from "@angular/core";
import { InputlistenerDirective } from './inputlistener.directive';

@Component({
   template: `<input type="number" fbInputlistener value="12345">` 
})
class TestInputComponent {
}

describe('InputlistenerDirective', () => {
   let component: TestInputComponent;
   let fixture: ComponentFixture<TestInputComponent>;
   let inputEl: DebugElement;


beforeEach(() => {
   TestBed.configureTestingModule({
     declarations: [TestInputComponent, InputlistenerDirective]
   });

   fixture = TestBed.createComponent(TestInputComponent);
   component = fixture.componentInstance;
   inputEl = fixture.debugElement.query(By.css('input'));
});

It('Directive should add class to test input', () => {
   inputEl.triggerEventHandler('keyup', null);
   fixture.detectChanges();

   expect(inputEl.nativeElement.classList.length).toBe(1);
 });
});