Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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中模拟单元测试的父类方法?_Angular_Unit Testing_Jasmine - Fatal编程技术网

如何在Angular中模拟单元测试的父类方法?

如何在Angular中模拟单元测试的父类方法?,angular,unit-testing,jasmine,Angular,Unit Testing,Jasmine,我正在为一个类编写一个单元测试,该类扩展另一个类并在运行时调用父方法。既然我不想处理父类,有没有办法模拟该方法调用?我尝试过多种方法,但在工作中一无所获 class A { doSomething(){ console.log(123); } } class B extends A { work(){ this.doSomething(); } } 如何在类B的单元测试中模拟此函数调用并返回其他内容 我尝试了以下方法: spyOn(b,'

我正在为一个类编写一个单元测试,该类扩展另一个类并在运行时调用父方法。既然我不想处理父类,有没有办法模拟该方法调用?我尝试过多种方法,但在工作中一无所获

class A {
   doSomething(){
       console.log(123);
     }
}

class B extends A {
    work(){
      this.doSomething();
  }
}
如何在类B的单元测试中模拟此函数调用并返回其他内容

我尝试了以下方法:

spyOn(b,'doSomething');

spyOn(Object.getPrototypeOf(b),'doSomething');

没有错误,它只是一直调用原始的父方法

您可以做的,但我不建议这样做,就是将父方法本身存根在类B中

我不推荐这种方法,因为您将在正在进行单元测试的类中存根一些内容。我更希望在这个父方法中完成一些事情

但是,如果您真的想存根该方法,您可以按照以下思路做一些事情:

describe('DataService', () => {
    let service: DataService;

    beforeEach(async(() => {
      TestBed.configureTestingModule({ providers: [DataService] });
    }));

    beforeEach(() => {
      service = TestBed.get(DataService); // would be inject in newer angular versions
    });

    it('test case 2', () => {
      spyOn(service as any, 'parentMethod').and.returnValue(5);
      expect(service.getData()).toEqual(5);
    });
});
其中
DataService
将是


@Injectable({
  providedIn: 'root'
})
export class DataService extends AbstractDataService {
  constructor() {
    super();
   }

  getData() {
    return this.parentMethod();
  }
}
AbstractDataService

@Injectable({
  providedIn: 'root'
})
export class AbstractDataService {
  constructor() { }

  parentMethod() {
    console.log('parent method');
    return null;
  }
}
也适用于组件。但同样:不建议在被测对象内部模拟方法

describe('AppComponent', () => {
    let component: AppComponent;
    let fixture: ComponentFixture<AppComponent>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [AppComponent, AbstractAppComponent],
            schemas: [NO_ERRORS_SCHEMA],
            bootstrap: [AppComponent]
        }).compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(AppComponent);
        component = fixture.componentInstance;
    });

    it('should mock method', () => {
      spyOn(component as any, 'abstractMethod').and.returnValue(10);

      fixture.detectChanges();
      expect(component.myMethod()).toEqual(10);
    });    
});
description('AppComponent',()=>{
let组件:AppComponent;

let fixture:ComponentFixture

很高兴向我们展示您到目前为止到底尝试了什么,以及具体的错误在哪里。Updated@Erbsenkoenigmakes有意义,但唯一的区别是在我的情况下,它是一个组件而不是一个服务。当您尝试从组件的父类存根一个方法时,这种方法不起作用。您能帮助我们吗这是吗?它的工作原理是一样的。你不会监视服务,而是监视组件内的方法。service=TestBed.get(DataService);这对组件不起作用。我已经尝试了spyOn(组件,'parentMethod');但它只调用原始方法当你使用测试床时,你会得到你的组件实例。你可以在上面存根方法,就像在你的服务上一样。看看stackblitz,我已经包括了一个组件版本。