how to test firestore methods

痴心易碎 提交于 2021-02-07 23:38:10

问题


I have the following issue:

I want to test the following methods who make use of Firestore:

databaseService.ts

import {Injectable} from '@angular/core';
import {AngularFirestore} from '@angular/fire/firestore';

@Injectable({
  providedIn: 'root'
})

export class DatabaseService {
  constructor(private afs: AngularFirestore) {
  }

  pushToDatabase(subject: string, key: string, object: Object): void {
    this.afs.collection(subject).doc(key).set(JSON.parse(JSON.stringify(object)));
  }

  async getData(subject: string, key: string): Promise<firebase.firestore.DocumentData> {
    const document = await this.afs.collection(subject).doc(key).ref.get();
    return document.data();
  }
  async deleteData(subject: string, key: string) {
    return await this.afs.collection(subject).doc(key).delete();
  }
}

databaseService.spec.ts

import {TestBed} from '@angular/core/testing';
import {DatabaseService} from './database.service';
import {AngularFirestore, AngularFirestoreModule} from '@angular/fire/firestore';
import {LoginComponent} from '../../components/login/login.component';
import {HttpClientTestingModule} from '@angular/common/http/testing';
import {InjectionToken} from '@angular/core';
import {AngularFireModule} from '@angular/fire';
import {async} from 'q';
import {AuthService} from '../auth/auth.service';
import {Router} from '@angular/router';
import {GithubService} from '../github/github.service';

export class AngularFirestoreMock {
}

describe('DatabaseService', () => {

  let sut: DatabaseService;
  let angularFireStore: AngularFirestore;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [DatabaseService, AngularFirestore]
    });

    TestBed.overrideComponent(LoginComponent, {
      set: {
        providers: [
          {provide: AngularFirestore, useClass: AngularFirestoreMock}
        ]
      }
    });

    sut = TestBed.get(DatabaseService);
    angularFireStore = TestBed.get(AngularFirestore);

  });

  describe('#pushToDatabase', async () => {
    it('should push data', function () {

    });
  });
});

But i don't know how to start. I found & installed the firestore-mock library. But even with this library i don't know how to proceed.

Can someone help me out?


回答1:


This works for me as a basic setup for testing AngularFirestore:

{
    const fakeAFS = jasmine.createSpyObj( 'AngularFirestore', [ 'collection' ]);
    fakeAFS.collection.and.returnValue(jasmine.createSpyObj( 'collection', [ 'doc', 'snapshotChanges', 'valueChanges' ]));
    fakeAFS.collection().doc.and.returnValue(jasmine.createSpyObj( 'doc', ['valueChanges']));
    fakeAFS.collection().doc().valueChanges.and.returnValue( of('FakeDocument'));
    fakeAFS.collection().doc().update.and.returnValue(Promise.resolve(null));

    TestBed.configureTestingModule({
        providers: [
            { provide: AngularFirestore, useValue: fakeAFS }
        ]
     });
}

Then, for the tests:

{
   it('Fake afs should work', () => {

        const collectionRef = fakeAFS.collection('TestCollectionName').doc('FAKE-ID').valueChanges();

        expect(fakeAFS.collection).toHaveBeenCalledWith('TestCollectionName');
        expect(fakeAFS.collection().doc).toHaveBeenCalledWith('FAKE-ID');
        expect(collectionRef).toBeTruthy();
    });
   it('Fake AFS Update should work', async () => {
       const testUpdate = await fakeAFS.collection().doc().update();

       expect(testUpdate).toBe(null);
    });
}


来源:https://stackoverflow.com/questions/54109970/how-to-test-firestore-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!