在构造函数中测试 rxjs 管道运算符
Testing rxjs pipe operator in constructor
我有 class 来处理来自 firestore 的 user/auth 操作。该服务工作正常,但是当我尝试向服务添加测试时如果失败并出现错误 TypeError: Cannot read property 'pipe' of undefined
这是我的服务
export class AuthService {
user$: Observable<User>;
constructor(private afAuth: AngularFireAuth, private afs: AngularFirestore) {
this.user$ = this.afAuth.authState.pipe(
switchMap(user => {
if (user) {
return this.afs.doc<User>(`user/${user.uid}`).valueChanges();
} else {
return of(null);
}
})
);
}
}
这是我的规范文件的一部分 class
describe('AuthService', () => {
beforeEach(() =>
TestBed.configureTestingModule({
providers: [
{ provide: AngularFireAuth, useValue: FireAutStub },
{ provide: AngularFirestore, useValue: FirestoreStub }
],
imports: [AngularFireModule, AngularFireAuthModule]
})
);
it('AuthService should be created', () => {
const service: AuthService = TestBed.get(AuthService);
expect(service).toBeTruthy();
});
});
关于为什么测试抛出任何想法 TypeError: Cannot read property 'pipe' of undefined
或者您有任何建议来更好地测试它或使服务更多 "testable"?
编辑:
这是 FireAutStub
const FireAutStub = {
collection: (name: string) => ({
doc: (_id: string) => ({
valueChanges: () => new BehaviorSubject({ foo: 'bar' }),
set: (_d: any) => new Promise((resolve, _reject) => resolve())
})
})
};
你可以在这里看到两个 classes https://gist.github.com/danielnv18/bfc5940f0bf078c77d895b5c34bf8a27
我认为你在嘲笑 FireAuthStub
不正确。它期望 authState
属性.
尝试将其设置为:
const FireAutStub = {
authState: new BehaviourSubject(true),
}
现在模拟 authState
为 true 和 false 以便后续测试命中 if
或 else
可能很棘手。您可能必须将它与某种类型的主题相关联,您可以对其执行 .next
并控制其值。
我有 class 来处理来自 firestore 的 user/auth 操作。该服务工作正常,但是当我尝试向服务添加测试时如果失败并出现错误 TypeError: Cannot read property 'pipe' of undefined
这是我的服务
export class AuthService {
user$: Observable<User>;
constructor(private afAuth: AngularFireAuth, private afs: AngularFirestore) {
this.user$ = this.afAuth.authState.pipe(
switchMap(user => {
if (user) {
return this.afs.doc<User>(`user/${user.uid}`).valueChanges();
} else {
return of(null);
}
})
);
}
}
这是我的规范文件的一部分 class
describe('AuthService', () => {
beforeEach(() =>
TestBed.configureTestingModule({
providers: [
{ provide: AngularFireAuth, useValue: FireAutStub },
{ provide: AngularFirestore, useValue: FirestoreStub }
],
imports: [AngularFireModule, AngularFireAuthModule]
})
);
it('AuthService should be created', () => {
const service: AuthService = TestBed.get(AuthService);
expect(service).toBeTruthy();
});
});
关于为什么测试抛出任何想法 TypeError: Cannot read property 'pipe' of undefined
或者您有任何建议来更好地测试它或使服务更多 "testable"?
编辑: 这是 FireAutStub
const FireAutStub = {
collection: (name: string) => ({
doc: (_id: string) => ({
valueChanges: () => new BehaviorSubject({ foo: 'bar' }),
set: (_d: any) => new Promise((resolve, _reject) => resolve())
})
})
};
你可以在这里看到两个 classes https://gist.github.com/danielnv18/bfc5940f0bf078c77d895b5c34bf8a27
我认为你在嘲笑 FireAuthStub
不正确。它期望 authState
属性.
尝试将其设置为:
const FireAutStub = {
authState: new BehaviourSubject(true),
}
现在模拟 authState
为 true 和 false 以便后续测试命中 if
或 else
可能很棘手。您可能必须将它与某种类型的主题相关联,您可以对其执行 .next
并控制其值。