本文介绍了如何测试Firestore方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

我想测试以下使用Firestore的方法:

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 () {

    });
  });
});

但是我不知道如何开始.我发现&安装了 firestore-mock 库.但是即使有了这个库,我也不知道如何进行.

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.

有人可以帮我吗?

推荐答案

这对我来说是测试AngularFirestore的基本设置:

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

然后,进行测试:

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

这篇关于如何测试Firestore方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:25