Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.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
Jquery ui 角度2+;jquery自动完成组件:无法调用spec文件内组件的方法_Jquery Ui_Angular_Jquery Autocomplete_Angular2 Testing - Fatal编程技术网

Jquery ui 角度2+;jquery自动完成组件:无法调用spec文件内组件的方法

Jquery ui 角度2+;jquery自动完成组件:无法调用spec文件内组件的方法,jquery-ui,angular,jquery-autocomplete,angular2-testing,Jquery Ui,Angular,Jquery Autocomplete,Angular2 Testing,我在angular2中使用了jquery自动完成方法,它调用服务从api获取数据。 这里是myComponent.ts export class myComponent { private myVar; private binding1; private binding2; constructor( @Inject(ElementRef) private elementRef: ElementRef,private _myService: MyService) { } public metho

我在angular2中使用了jquery自动完成方法,它调用服务从api获取数据。 这里是myComponent.ts

export class myComponent {
private myVar;
private binding1;
private binding2;
constructor( @Inject(ElementRef) private elementRef: ElementRef,private _myService: MyService) {

}
public method1() { return this.myVar.val().toUpperCase(); }
public method2() { return this.myVar.val(""); }

private ngOnInit() {
    var _this = this;
    this.myVar = $(this.elementRef.nativeElement).children().eq(0).autocomplete({
        source: function(request,callback){
            _this.mainMethod(request,callback);
        },
delay:0,
select: (event, ui) => {
// …},
open: function(e,ui)
{
    //…
},
appendTo: $('body')
});

//renderMethod add data to the view using $().append()

public mainMethod (res, callback) { //gets called from inside autocomplete
if(condition 1) {
//renders data from service by calling methodOnService()
//returns binding1 and binding2  which gets rendered in view (see html)
}
else {
//call anotherMethod();
//sets binding1 and binding2  which gets rendered in view (see html)

}
}
public anotherMethod() {
//…
}
myComponent.html

<input type="text" value="{{binding1}}" size="{{binding2}}" maxlength="94"><span></span>
即使调用method1和method2,我也无法在规范中模拟this.myVar?我应该如何测试各种方法?

实际上并不是一个答案(并不真正理解问题),而是一些建议(参见代码中的注释):

导出类myComponent{
私有myVar;
私人绑定1;
私人绑定2;
//删除“@Inject(ElementRef)”,因为当类型为
//也有'ElementRef'`
构造函数(private-elementRef:elementRef,private\u-myService:myService){
}
public method1(){返回this.myVar.val().toUpperCase();}
public method2(){返回this.myVar.val(“”;}
私人恩戈尼尼特(){
//var_this=这个;
this.myVar=$(this.elementRef.nativeElement).children().eq(0).autocomplete({

来源:(请求,回调)=>{//{//你说的mock
this.myVar
是什么意思?
this.myVar
存储jQuery对象,调用spec文件中的
method1
,它说找不到
.val()
的未定义。所以
这个.myVar
没有被什么模仿?你为什么期望它被模仿?我只想在spec文件中调用
method1
。我所说的模仿是指它包含在
ngOnInit
中返回的jQuery对象。我更新了我的spec文件。你现在明白这个问题了吗?我没有看到你做任何会有问题的事情d导致
myVar
method1
被嘲笑。我不明白:-/
fit(‘my component test file’,inject([TestComponentBuilder, MyComponent, ElementRef], (tcb:TestComponentBuilder) => {
        tcb.createAsync(MyComponent)
            .then((fixture) => {

                const element = fixture.debugElement.nativeElement;
                const instance = fixture.componentInstance;
                console.log("component instance", fixture.componentInstance);
                fixture.componentInstance.binding2 =12;
                fixture.componentInstance.binding1 = 'aapl';

                spyOn(instance, "methodOnService");
                spyOn(instance,"anotherMethod");
                fixture.detectChanges(); //while debugging, it invokes 'ngOnInit' method but doesn't invoke autocomplete method
                fixture.componentInstance.symbol= 'aa';
                fixture.componentInstance.binding2 =12;
                fixture.detectChanges(); //it doesn't even invoke 'ngOnInit'


                 expect(instance.methodOnService.calls.any()).toEqual(true); //error : Expected false to equal true
                expect(instance.anotherMethod).toHaveBeenCalled();
// error :Expected spy anotherMethod to have been called.