Angular 单元测试 - 帮助监视 Angularfire arrayUnion 和 arrayRemove

问题描述 投票:0回答:1

我有两个类似的函数来创建/删除数组中的项目,一旦我从 firestore 中调用 arrayUnion 和 arrayRemove 函数,单元测试就会崩溃。该错误似乎表明我正在跳过模拟提供程序并尝试使用真实的提供程序。不幸的是,我无法弄清楚我需要模拟什么来拦截 arrayUnion 和 arrayRemove 调用,这样它们就不会导致下面的错误。

我注意到在我的服务中,当我导入函数时,它们来自两个不同的地方,但我不太明白。

import { arrayRemove, arrayUnion } from '@angular/fire/firestore';
import { AngularFirestore } from '@angular/fire/compat/firestore';

错误

    Error: Either AngularFireModule has not been provided in your AppModule (this can be done manually or implictly using provideFirebaseApp) or you're calling an AngularFire method outside of an NgModule (which is not supported).

服务.ts

  createCharacterPartyLink(characters: any[], name: string, partyId: string) {
    const characterPartyCollectionRef = this.afs.collection<character>('users/' + this.uid + '/Characters/')
    characters.forEach((char: any) => {
      characterPartyCollectionRef.doc(char.characterid).update(
        { memberOf: arrayUnion({ partyId: partyId, partyName: name }) }
      )})}

服务.spec.ts

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        { provide: AngularFirestore, useValue: AngularFirestoreMock },
        { provide: AngularFireAuth, useValue: AngularFireAuthMock }
      ]
    });
    service = TestBed.inject(PartyService);
  });
      
it('createCharacterPartyLink Work as Expected', () => {
    const updateObj = {
      update: function () {
        return { 
          arrayUnion: ()=> {

        }}
      },
    };

    const updateSpy = spyOn(updateObj, 'update');

    AngularFirestoreMock.collection.and.returnValue({
        doc: () => {
          return updateObj
        }
      }
    )

    service.createCharacterPartyLink(mockPartyMembers, 'Test Party', '1234')

    expect(updateSpy).toHaveBeenCalled()

  });
angular unit-testing jasmine angularfire
1个回答
0
投票

几天后回到这个问题,我意识到这与我在规范文件中的其他测试中调用 jasmine.createSpyObj 的方式有关。清理模拟后,以下测试非常适合我原来的服务实现。

arrayUnion 规范

  it('createCharacterPartyLink Work as Expected', () => {

    const updateObj = {
      update: function () {
        return { 
          arrayUnion: ()=> {

        }}
      },
    };

    const updateSpy = spyOn(updateObj, 'update');

    AngularFirestoreMock.collection.and.returnValue({
        doc: () => {
          return updateObj
        }
      }
    )

    service.createCharacterPartyLink(mockPartyMembers, 'Test Party', '1234')

    expect(updateSpy).toHaveBeenCalled()

  });

数组删除规格

  it('removeCharacterPartyLink Work as Expected', () => {

    const updateObj = {
      update: function () {
        return { 
          arrayRemove: ()=> {

        }}
      },
    };

    const updateSpy = spyOn(updateObj, 'update');

    AngularFirestoreMock.collection.and.returnValue({
        doc: () => {
          return updateObj
        }
      }
    )

    service.removeCharacterPartylink(mockPartyMembers, 'Test Party', '1234') 

    expect(updateSpy).toHaveBeenCalled()

  });
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.