gpt4 book ai didi

Angular firebase - 使用 angularFireAuth 对服务进行单元测试

转载 作者:行者123 更新时间:2023-12-02 20:18:42 26 4
gpt4 key购买 nike

我有一个核心服务,它获取 firebase.auth obj 的实例并使用 getter 返回它。它很简单,如下所示:

export class AuthService {

...
constructor(private afAuth: AngularFireAuth,
private afs: AngularFirestore) {
...
}

public getAuthInstance(): FirebaseAuth {
return this.afAuth.auth;
}
}

现在我有一个 LoginService,它需要 AuthService 来登录我的应用程序。

export class LoginService {

constructor(private authService: AuthService) {
}

public login(email, password): Promise<UserCredential | string> {
return this.authService.getAuthInstance().signInWithEmailAndPassword(email, password)
.catch(() => Promise.reject('Login failed.'));
}

也不太复杂。但现在我想测试我的登录组件。 - 特别是login方法。我无法模拟 signInWithEmailAndPassword。我基本上尝试了一切:

  • jasmine.spies
  • 自己创建了假对象。

测试类如下所示:

  ...
const authStub: AuthService = jasmine.createSpyObj('AuthService', ['getAuthInstance']);
const fireStub = {
auth: {
signInWithEmailAndPassword() {
return Promise.resolve();
}
}
};

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{provide: AuthService, useValue: authStub},
{provide: AngularFireAuth, useValue: fireStub},
LoginService
]
});
});

const spy = it('should call signInWithPasswordAndEmail', inject([LoginService], (service: LoginService) => {
(<jasmine.Spy>authStub.getAuthInstance).and.returnValue({username: 'test'});
spyOn(fireStub.auth, 'signInWithEmailAndPassword').and.callThrough();


service.login(email, password).then(() => expect(spy).toHaveBeenCalledTimes(1));
}));

谁能帮我让这个该死的测试变绿吗? xD^^错误消息。抛出的是: this.getAuthInstance().signInWithEmail.... 不是函数

最佳答案

经过一番尝试和错误后,我使用 AngularFireAuthAngularFireStore 间接模拟了 AuthService,测试配置如下所示:

describe('LoginService', () => {

const email: string = 'email';
const password: string = 'password';

const authStub: any = {
authState: {},
auth: {
signInWithEmailAndPassword() {
return Promise.resolve();
}
}
};

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{provide: AngularFireAuth, useValue: authStub},
{provide: AngularFirestore},
LoginService
]
});
authStub.authState = of(null);
});

it('should call signInWithPasswordAndEmail', inject([LoginService], (service: LoginService) => {
const mock = TestBed.get(AngularFireAuth);
const spy = spyOn(authStub.auth, 'signInWithEmailAndPassword').and.callThrough();
mock.auth = authStub.auth;

service.login(email, password);

expect(spy).toHaveBeenCalledWith(email, password);
}));
});

它完成了工作。如果有人有更好的解决方案,我很高兴知道。

关于Angular firebase - 使用 angularFireAuth 对服务进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51894948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com