使用ng test
时遇到问题:由于此错误,我的测试无法进行:
this.TerrariumsCollection.valueChanges()不是函数
仅当我使用ng test命令时才会出现此问题。否则,一切似乎都很好。
这是我的TS文件:
TerrariumsCollection : AngularFirestoreCollection<Terrarium>;
Terrarium : Observable<Terrarium[]>
constructor(private afs : AngularFirestore) {
this.TerrariumsCollection = this.afs.collection('Terrarium');
this.Terrarium = this.TerrariumsCollection.valueChanges();
}
这是我的单元测试规范:
import { TestBed } from '@angular/core/testing';
import { MesTerrasService } from './mes-terras.service';
import { BehaviorSubject } from 'rxjs';
import { AngularFirestore, AngularFirestoreModule, AngularFirestoreCollection } from 'angularfire2/firestore';
import { AngularFireModule } from 'angularfire2';
import { environment } from '../environments/environment';
export const firebaseConfig = environment.firebaseConfig;
describe('MesTerrasService', () => {
beforeEach(() => TestBed.configureTestingModule({
providers: [
{ provide: AngularFirestore, useValue: FirestoreStub },
],
imports: [
AngularFireModule.initializeApp(firebaseConfig),
AngularFirestoreModule,
AngularFirestoreCollection
]
}));
it('should be created', () => {
const service: MesTerrasService = TestBed.get(MesTerrasService);
expect(service).toBeTruthy();
});
const FirestoreStub = {
collection: (name: string) => ({
doc: (_id: string) => ({
valueChanges: () => new BehaviorSubject({ foo: 'bar' }),
set: (_d: any) => new Promise((resolve, _reject) => resolve()),
}),
}),
};
});
当我使用ng serve
时,没问题,它可以工作。
我将我的app.module和我在规范中导入的所有模块进行了比较,看起来都不错。我可以忘记一个吗?
能帮我吗?