首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swUpdate 7中断单元测试,不能模拟/重写测试(PWA)

swUpdate 7中断单元测试,不能模拟/重写测试(PWA)
EN

Stack Overflow用户
提问于 2019-10-23 10:40:27
回答 1查看 946关注 0票数 1

下面是实现swUpdate的组件:

代码语言:javascript
复制
public thereIsUpdate: boolean;

constructor(public swUpdate: SwUpdate) {
    this.checkForUpdates();
}

checkForUpdates() {
  this.swUpdate.checkForUpdate().then(() => {
    this.swUpdate.available.subscribe(swEvt => {
      // an update is available
      this.thereIsUpdate = true;
    });
  });
}

下面是我的单元测试:

代码语言:javascript
复制
class MockSwUpdate {
  available = new Subject<{ available: { hash: string } }>().asObservable();
  activated = new Subject<{ current: { hash: string } }>().asObservable();

  public checkForUpdate(): Promise<void> {
    console.log('This is not working');
    return new Promise((resolve) => resolve());
  }
  public activateUpdate(): Promise<void> {
    return new Promise((resolve) => resolve());
  }

  constructor(public isEnabled: boolean) {
  }
}

class MockApplicationRef {
  isStable = new Subject<boolean>();
}

describe('xComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        xComponent
      ],
      imports: [
        RouterTestingModule,
        ServiceWorkerModule.register('', {enabled: false})
      ],
      providers: [
        {provide: SwUpdate, useValue: MockSwUpdate},
        {provide: ApplicationRef, useClass: MockApplicationRef}
      ]
    }).compileComponents();
  }));
}

我的问题是不能为swUpdate**做“模拟”,不知怎么它不起作用。**

不知何故,即使我指定了SwUpdate,也不会对它进行模拟。

当我运行ng test时,它显示了以下错误:

代码语言:javascript
复制
Uncaught TypeError: this.swUpdate.checkForUpdate is not a function thrown
    TypeError: this.swUpdate.checkForUpdate is not a function

请注意:模拟是,仅不适用于

EN

回答 1

Stack Overflow用户

发布于 2019-11-02 08:16:39

为了测试类似的服务,我在每个测试中手动创建了一个实例,而不是使用TestBed,并将其作为模拟swUpdate服务传递。

代码语言:javascript
复制
  let mockSwUpdate: any;

  beforeEach(() => {
    mockSwUpdate = {
      isEnabled: false,
      activated: EMPTY,
      available: EMPTY,
      checkForUpdate: () => of(null).toPromise<void>(),
      activateUpdate: () => of(null).toPromise<void>()
    };
  });

  beforeEach(() => {
    mockSwUpdate.isEnabled = true;
  });

  it('should call checkForUpdates', () => {
    spyOn(mockSwUpdate, 'checkForUpdate').and.returnValue(null);

    const service: ServiceWorkerService = new ServiceWorkerService(
      mockSwUpdate as any
    );

    expect(mockSwUpdate.checkForUpdate).toHaveBeenCalled();
  });

  it('should return true is updateAvailbleEmits', (done: DoneFn) => {
    mockSwUpdate.available = of(true);

    const service: ServiceWorkerService = new ServiceWorkerService(
      mockSwUpdate as any
    );

    service.updateAvailable$.subscribe(result => {
      expect(result).toBeTruthy();
      done();
    });
  });
  ...
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58521010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档