【问题标题】:Angular 7 swUpdate breaks unit testing and cannot be mock / override for testing (PWA)Angular 7 swUpdate 打破了单元测试,不能模拟/覆盖测试(PWA)
【发布时间】:2019-10-23 10:40:27
【问题描述】:

这是我实现 swUpdate 的组件:

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;
    });
  });
}

这是我的单元测试:

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 时,它显示此错误:

Uncaught TypeError: this.swUpdate.checkForUpdate is not a function thrown
    TypeError: this.swUpdate.checkForUpdate is not a function

请注意:模拟仅不适用于SwUpdate

【问题讨论】:

  • 你找到了解决办法?
  • @Stephane 我在我的应用程序组件中使用这个服务工作者。所以目前我只是在 app.component.spec.ts 中评论整个单元测试文件。
  • 所以我仍然可以为其他组件运行ng test

标签: angular service-worker progressive-web-apps angular-unit-test angular-pwa


【解决方案1】:

为了测试类似的服务,我为每个测试手动创建了一个实例,而不是使用 TestBed 并像任何一样传入一个模拟 swUpdate 服务。

  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();
    });
  });
  ...

【讨论】:

  • 嗨@JayChase 我的问题更多的是“SwMock 不工作”。不知何故,单元测试没有使用我创建的“模拟”。我想测试使用服务工作者的组件,而不是测试服务工作者本身。你能帮忙吗?
猜你喜欢
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 2015-02-27
  • 1970-01-01
相关资源
最近更新 更多