Angular2 jasmine测试因内联模板0:0失败,原因是:null不是对象

Angular2 jasmine测试因内联模板0:0失败,原因是:null不是对象,angular,jasmine,Angular,Jasmine,我有一个基本指令,它作用于输入框以限制允许的最低值,在这个测试用例中不小于零 但是,我无法通过此测试,因为内联模板:0:0因:null不是对象而导致错误 这似乎与ngModel有关,但我在YouBooks、FaceTube或Yahoogles的互联网站上的搜索都没有找到答案 提前感谢您的帮助 import { Directive, Input, ElementRef, HostListener } from '@angular/core'; import { NgModel } from '@a

我有一个基本指令,它作用于输入框以限制允许的最低值,在这个测试用例中不小于零

但是,我无法通过此测试,因为内联模板:0:0因:null不是对象而导致错误

这似乎与ngModel有关,但我在YouBooks、FaceTube或Yahoogles的互联网站上的搜索都没有找到答案

提前感谢您的帮助

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

@Directive({
selector: '[dleGbMinValue]',
providers: [NgModel]
})

export class MinValueDirective {

@Input('dleGbMinValue') minValue : string;

constructor(private el: ElementRef, private model: NgModel) { }

@HostListener('change') onChange() {
if (this.el.nativeElement.value && parseFloat(this.el.nativeElement.value) < parseFloat(this.minValue)) {
  this.model.valueAccessor.writeValue(parseFloat(this.minValue));
  this.model.viewToModelUpdate(parseFloat(this.minValue));
  }
}

@HostListener('blur') onBlur() {
if (!this.el.nativeElement.value || parseFloat(this.el.nativeElement.value) < parseFloat(this.minValue)) {
  this.model.valueAccessor.writeValue(parseFloat(this.minValue));
  this.model.viewToModelUpdate(parseFloat(this.minValue));
   }
  }
}
import{Directive,Input,ElementRef,HostListener}来自“@angular/core”;
从'@angular/forms'导入{NgModel};
@指示({
选择器:“[dleGbMinValue]”,
提供者:[NgModel]
})
导出类MinValueDirective{
@Input('dleGbMinValue')minValue:string;
构造函数(私有el:ElementRef,私有模型:NgModel){}
@HostListener('change')onChange(){
if(this.el.nativeElement.value&&parseFloat(this.el.nativeElement.value)
这里是测试代码

从“/min value.directive”导入{MinValueDirective};
@组成部分({
模板:``
})
类TestMinValueDirectiveComponent{
}
描述('指令:MinValueDirective',()=>{
let组件:TestMinValueDirectiveComponent;
let夹具:组件夹具;
let inputEl:DebugElement;
在每个之前(()=>{
TestBed.configureTestingModule({
声明:[TestMinValueDirectiveComponent,MinValueDirective]
});
fixture=TestBed.createComponent(TestMinValueDirectiveComponent);
组件=fixture.componentInstance;
inputEl=fixture.debugElement.query(By.css('input'));
fixture.detectChanges();
});
它('更改时应在最小值处停止',()=>{
expect(inputEl.nativeElement.value).toEqual(“”);
inputEl.nativeElement.value='-5';
inputEl.triggerEventHandler('change',null);
fixture.detectChanges();
expect(inputEl.nativeElement.value).toEqual(0);
});
});

您不需要在
MinValueDirective
中提供
NgModel

改用
FormsModule
中的
ngModel
指令

@Component({ 
  template: `<input type="number" ngModel dleGbMinValue="0">` // add ngModel
})
class TestMinValueDirectiveComponent {}
然后为了防止错误:
内联模板:0:0由以下原因引起:无法从
NumberValueAccessor
中读取null的属性“target”

inputEl.triggerEventHandler('change', null);


您可以在

中找到完整的测试,您不需要在
MinValueDirective
中提供
NgModel

改用
FormsModule
中的
ngModel
指令

@Component({ 
  template: `<input type="number" ngModel dleGbMinValue="0">` // add ngModel
})
class TestMinValueDirectiveComponent {}
然后为了防止错误:
内联模板:0:0由以下原因引起:无法从
NumberValueAccessor
中读取null的属性“target”

inputEl.triggerEventHandler('change', null);


您可以在

中找到一个完整的测试,棒极了!非常感谢,太棒了!非常感谢你。