2

我在 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>

我发现很难测试代码,因为它混合了 angular 和 jquery(我知道这不好)。但是现在,我想从我的测试文件中调用 method1、method2、mainMethod、anotherMethod 以获得更多的代码覆盖率。

myComponent.spec.ts文件:

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.

即使对于调用 method1 和 method2,我也无法在规范中模拟 this.myVar 吗?我应该如何测试各种方法?

4

1 回答 1

0

实际上不是答案(不太了解问题),而是一些建议(请参阅代码中的注释):

export class myComponent {
  private myVar;
  private binding1;
  private binding2;

  // remove `@Inject(ElementRef)` because it's redundant whent the type is
  // also `ElementRef`
  constructor(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: (request,callback) => { // <<=== use arrow instead of `function`
            this.mainMethod(request,callback);
        },
    delay:0,
    select: (event, ui) => {
    // …},
    open: (e,ui) => { // <<=== use arrow instead of function
      //…
    },
    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() {
  //…
  }
}
于 2016-06-10T06:23:06.980 回答