要解决Angular 16中使用Jasmine对signInWithEmailAndPassword(来自angular/fire/auth)进行spy的工作异常的问题,可以尝试以下步骤:
import { AngularFireAuth } from '@angular/fire/auth';
import { TestBed } from '@angular/core/testing';
const mockAuth = jasmine.createSpyObj('AngularFireAuth', ['signInWithEmailAndPassword']);
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
{ provide: AngularFireAuth, useValue: mockAuth }
]
}).compileComponents();
}));
it('should call signInWithEmailAndPassword', () => {
const email = 'test@example.com';
const password = 'password';
mockAuth.signInWithEmailAndPassword.and.returnValue(Promise.resolve(true));
// 调用测试组件的方法,其中会调用signInWithEmailAndPassword方法
// 并在方法内部处理返回值
expect(mockAuth.signInWithEmailAndPassword).toHaveBeenCalledWith(email, password);
});
通过这些步骤,您应该能够正确创建一个mock spy对象,并且能够在测试中对signInWithEmailAndPassword方法进行模拟和检查。请根据您的具体代码和需求进行相应的调整。