我在有角度的web应用程序内部实现了Jasmine / Karma。 当我进行测试时,由于firebase,我不断收到错误消息。 我收到错误消息:“无法读取未定义的属性Firestore” 这是我的错误-> enter image description here
这是我的业力配置文件
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, '../coverage'),
reports: ['html', 'lcovonly'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
这是我的测试规格文件的设置方式。我说的很简单,还没有做任何测试
describe('FirebaseService', () => {
beforeEach(() => TestBed.configureTestingModule({
providers:[ { provide: AngularFireAuth }, { provide: AngularFirestore
}]
}));
it('should be created', () => {
const service: FirebaseService = TestBed.get(FirebaseService);
expect(service).toBeTruthy();
});
});
我的firebaseService
import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';
import { User } from 'src/app/user/user';
@Injectable({
providedIn: 'root'
})
export class FirebaseService {
dbUser;
dbUserDoc;
constructor(private db: AngularFirestore) {
db.firestore.settings({ timestampsInSnapshots: true, });
}
addCompetitionIdToUser(compId, uId) {
const userRef: AngularFirestoreDocument<User> =
this.db.doc(`users/${uId}`);
userRef.update({
competitionIds: compId
});
}
}