I need to test a function and the only test case I need help with is to check if the dataCollection.add()
method has been called . I presume a spy is needed here, but any recommendations will be much appreciated.
Here is the function I need to test containing the dataCollection.add()
method:
...
constructor(private _firebase: AngularFirestore, private _read: ReadService) {}
createRootCollectionDocument$(collection: string, data: any): Observable<firebase.firestore.DocumentReference> {
const testt = this._firebase.collection(collection);
const dataCollection: AngularFirestoreCollection<any> = this._firebase.collection(collection);
return Observable.from(
dataCollection.add({ ...data }).catch((err: firebase.firestore.FirestoreError) => {
throw err.message;
})
);
}
Here is my test case as yet:
...
beforeEach(() => {
service = TestBed.get(CrudService);
}
it('uses the firebase collection.add method', () => {
return service.createRootCollectionDocument$(collectionStub, dataStub).toPromise().then((result) => {
...
})
});
...
Thanks in advance!
You can stub out this._firebase.collection
to return a mock object and then see that its add
function was called.
it('uses the firebase collection.add method', () => {
let mockAdder = jasmine.createSpyObj("adder", ["add"]);
jasmine.spyOn(service["_firebase"], "collection").andReturn(mockAdder);
return service.createRootCollectionDocument$(collectionStub, dataStub).toPromise().then((result) => {
expect(mockAdder.add).toHaveBeenCalled();
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.