简体   繁体   English

angular jasmine 测试抽象类与其他抽象类

[英]angular jasmine test abstract class with other abstract class

I have this scenario problem:我有这个场景问题:

In a Ionic/Angular project(ionic v3/Angular v5) I have the component SuppliedVanContainerComponent which extends the abstract class AbstractLoadingClass and this class extends another abstract class AbstractSubscriberClass .在 Ionic/Angular 项目(ionic v3/Angular v5)中,我有一个组件SuppliedVanContainerComponent ,它扩展了抽象类AbstractLoadingClass ,这个类扩展了另一个抽象类AbstractSubscriberClass

The AbstractLoadingClass use the LoadingController an ionic-angular service for showing the spinner and it used by SuppliedVanContainerComponent when call the showLoading methods. AbstractLoadingClass使用LoadingController一个离子角度服务来显示微调器,它在调用 showLoading 方法时由 SuppliedVanContainerComponent 使用。

The problem: When I can use unit test with jasmine it show me those error:问题:当我可以对 jasmine 使用单元测试时,它会向我显示这些错误:

TypeError: Cannot read properties of undefined (reading '_getPortal')类型错误:无法读取未定义的属性(读取“_getPortal”)

And when I go to mock the LoadingController with jasmine.createSpyObj, it show me another error and is:当我用 jasmine.createSpyObj 模拟LoadingController时,它显示了另一个错误,是:

TypeError: this.loading.present is not a function类型错误:this.loading.present 不是函数

How can I fix it or can I test with abstract classes?我该如何修复它或者我可以使用抽象类进行测试?

SuppliedVanContainerComponent提供的VanContainer组件

    export class SuppliedVanContainerComponent extends AbstractLoadingClass
      constructor(loadingCtrl: LoadingController) {
            super(loadingCtrl);
        }

      ngOnInit(){
        this.showLoading();
      }

AbstractLoadingClass抽象加载类

import {Loading, LoadingController} from 'ionic-angular';
import {LoadingOptions} from 'ionic-angular/umd/components/loading/loading-options';

import {AbstractSubscriberClass} from './abstract-subscriber.class';

export abstract class AbstractLoadingClass extends AbstractSubscriberClass {
  private loading: Loading;
  
  protected constructor(public loadingCtrl: LoadingController) {
    super();
  }
  
  private getCreateLoading(opts?: LoadingOptions) {
    return this.loadingCtrl.create(opts);
  }
  
  showLoading() {
    this.loading = this.getCreateLoading({content: ''});
    this.loading.present();
    this.loading.setContent('');
  }

  hideLoading() {
    this.loading.dismiss();
  }
}

The jasmine test with error茉莉花测试有误

...
  let fakeLoadingController = jasmine.createSpyObj<LoadingController>('LoadingController', {
    create: {content: ''},
  })
...
{provide: LoadingController, useValue: fakeLoadingController},

The error:错误:
错误画面

Thanks @AliF50 the i have two solutions and i post it, First Solution:感谢@AliF50,我有两个解决方案,我将其发布,第一个解决方案:

....
let fakeLoadingController = jasmine.createSpyObj<LoadingController>('LoadingController', ['create'])
......
providers: [
....
   {provide: LoadingController, useValue: fakeLoadingController},
]
....
beforeEach(() => {
    fixture = TestBed.createComponent(SuppliedVanContainerComponent);
    fakeLoadingController.create.and.returnValue({present: () => null, setContent: () => null, dismiss: () => null});
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

And it's work as simple and without spy or test with LoaderController (just mocking).它的工作非常简单,无需使用 LoaderController 进行间谍或测试(只是模拟)。

If you would work with Loading and spy , to be set in this mode:如果您要使用 Loading 和 spy ,请在此模式下设置:

.....
  let fakeLoadingController = jasmine.createSpyObj<LoadingController>('LoadingController', ['create'])
  let fakeLoadingObject = jasmine.createSpyObj<Loading>('Loading', ['present', 'setContent', 'dismiss']);
.....
providers: [
...
        {provide: LoadingController, useValue: fakeLoadingController},
        {provide: Loading, useValue: fakeLoadingObject},
]
....
  beforeEach(() => {
    fixture = TestBed.createComponent(SuppliedVanContainerComponent);
    dailyReportsModel = TestBed.get(DailyReportsModel);
    fakeLoadingController.create.and.returnValue(fakeLoadingObject);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });
  ....
  it('test fakeLoadingObject', () => {
    expect(fakeLoadingObject.present).toHaveBeenCalled();
  })

Thanks at @AliF50感谢@AliF50

I think you're mocking it incorrectly, try this:我认为你嘲笑它是错误的,试试这个:

let fakeLoadingController = jasmine.createSpyObj<LoadingController>('LoadingController', ['create']);
...
{provide: LoadingController, useValue: fakeLoadingController},
...
// before fixture = ..., add this line
fakeLoadingController.create.and.returnValue({ present: () => null, setContent: () => null, dismiss: () => null });
// Or if you want spies on present, setContent, dismiss, you could do
let fakeLoadingObject = jasmine.createSpyObj<any>('FakeLoadingObject', ['present', 'setContent', 'dismiss']);
// then the line above becomes
fakeLoadingController.create.and.returnValue(fakeLoadingObject);
// And then you should have a handle of something like this:
expect(fakeLoadingObject.present).toHaveBeenCalled();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM