gpt4 book ai didi

javascript - 测试 AngularFireAuth Wrapper,不使用 AngularFireAuth 模拟

转载 作者:行者123 更新时间:2023-12-04 00:38:03 27 4
gpt4 key购买 nike

我想要一个包装 AngularFireAuth 服务的 AuthWrapper 服务。像这样。

// EDIT: Adding some import statements.
import {TestBed} from '@angular/core/testing';
import { AuthWrapperService } from './auth-wrapper.service';
import {AngularFireModule} from '@angular/fire';
import {AngularFireAuth, AngularFireAuthModule} from '@angular/fire/auth';
import {environment} from '../environments/environment';

@Injectable({
providedIn: 'root'
})
export class AuthWrapper {
constructor(public afAuth: AngularFireAuth) { }

isAuthenticated(): Observable<firebase.User> {
return this.afAuth.user;
}

createUserWithEmailAndPassword(email: string, password: string): Promise<String> {
let authPromise: Promise<firebase.auth.UserCredential> =
this.afAuth.auth.createUserWithEmailAndPassword(email, password);
return authPromise
.then((value) => {
return value.user.email;
})
.catch(function(error) {
return error.message;
});
}
}

我想要一个包装器,以便我可以测试与 AngularFireAuth 的连接。其他测试模拟 AuthWrapper。

( 不模拟 AngularFireAuth 的原因:假设我模拟 AngularFireAuth,然后我确定模拟的返回值。然后我说我理解这些值会是什么样子。如果没有测试这些值就假设这个是不安全的调用后端。说谷歌改变了真正的 AngularFireAuth 方法的结果,然后我将被迫改变我的每个 AngularFireAuth 模拟的结果。相反,最好将 AngularFireAuth 包装在包装器中,并将包装器的方法更改为符合谷歌的变化。)

我在 Karmine 和 Jasmine 中的测试导致“异步回调未在 5000 毫秒内调用错误”。我知道用户已对其进行签名,因为第一个 expect 通过了,但是我如何让第二个 expect 起作用?:

describe('AuthWrapperService', () => {
let fireAuthService: AngularFireAuth;
let password = "dcbA4321!";

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
AngularFireModule.initializeApp(environment.firebase),
AngularFireModule,
AngularFireAuthModule
],
providers: [AngularFireAuth, AuthWrapperService],
declarations: []
})
fireAuthService = TestBed.get(AngularFireAuth);
});

it('should sign in user', async() => {
const email = "anyemail@gmail.com";
let authWrap = new AuthWrapperService(fireAuthService);
let userEmail = await authWrap.createUserWithEmailAndPassword(email, password);
expect(userEmail).toBe("anyemail@gmail.com");
let obs = authWrap.isAuthenticated();
let promise = obs.toPromise();
let user = await promise.then((result) => {return result});
expect(user.email).toBe("anyemail@gmail.com");
});
});

我还没有看到任何 Angular 测试代码,其中没有模拟 AngularFireAuth。

最佳答案

这些是对我有效的测试,时间间隔为 5000 毫秒。我确实在预期行中使用“bad@gmail.com”对它们进行了测试,以确保预期行确实在运行,是的,预期行实际上正在通过。我打算只定期运行 AuthWrapperService 测试,因为它们会访问数据库。我将在其他测试中模拟 AuthWrapperService。在运行这些测试之前,我通过 firebase 控制台手动删除了 firebase 数据库中的现有用户。我确实一次运行了这两个测试,(在一个 describe block 内)。

it('should create new user', async() => {
const annEmail = "ann@gmail.com";
let authWrap = new AuthWrapperService(fireAuthService);
await authWrap.createUserWithEmailAndPassword(annEmail, password).then(
((testEmail) => {
expect(fireAuthService.auth.currentUser.email).toBe(annEmail);
})
)
await fireAuthService.auth.signOut();
});

it('should return error message when user already exists', async() => {
const email = 'ben@gmail.com';
const error = 'already in use';
let authWrap = new AuthWrapperService(fireAuthService);
await authWrap.createUserWithEmailAndPassword(email, password)
.then((value: String) => {
expect(fireAuthService.auth.currentUser.email).toBe(email);
});
await fireAuthService.auth.signOut();
expect(fireAuthService.auth.currentUser).toBeNull();
await authWrap.createUserWithEmailAndPassword(email, password)
.then((value: String) => {
expect(value).toContain('already in use');
});
await fireAuthService.auth.signOut();
});

我决定使用 fireAuthService.auth.currentUser.email 而不是 AuthWrapperService 的 isAuthenticated() 方法来直接测试我的 AuthWrapperService 的 createUserWithEmailAndPassword() 方法是否在 firebase 中创建了一个用户。

我意识到这不是单元测试,但我决定进行集成测试比模拟我不拥有的方法更好。

以下测试在 5000 毫秒内对我也有效,并且使用了 isAuthenticated() 方法。

it('should create user cat', async() => {
const catEmail = "cat@gmail.com";
let authWrap = new AuthWrapperService(fireAuthService);
let userEmail = await authWrap.createUserWithEmailAndPassword(catEmail, password);
expect(userEmail).toBe(catEmail);
await authWrap.isAuthenticated().subscribe({
next(user) {
expect(user.email).toBe(catEmail);
}
});
console.log("cat, timeinterval: " + jasmine.DEFAULT_TIMEOUT_INTERVAL);
await fireAuthService.auth.signOut();
});

以下“段位测试”导致超时。我试了一次 29000ms 就没时间了。此测试的大多数运行时间为 10 秒。很多时候,但并非总是如此,如果用户在测试运行之前已经是用户(因此在测试期间并没有真正创建用户),则此测试会正确通过。有一次,当用户在测试之前没有创建时,这个测试正确通过了,但是我在测试之前没有用户存在的情况下运行了很多次,但都失败了。我不确定为什么会这样。

it('should create user dan', done => {
const datEmail = "dan@gmail.com";
let authWrap = new AuthWrapperService(fireAuthService);
authWrap.isAuthenticated().subscribe(
{
next(user){
expect(user.email).toBe(datEmail);
done();
}
}
);
authWrap.createUserWithEmailAndPassword(datEmail, password);
console.log("dan, timeinterval: " + jasmine.DEFAULT_TIMEOUT_INTERVAL);
});

关于javascript - 测试 AngularFireAuth Wrapper,不使用 AngularFireAuth 模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60049969/

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