- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想要一个包装 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/
我想要一个包装 AngularFireAuth 服务的 AuthWrapper 服务。像这样。 // EDIT: Adding some import statements. import {Test
这个问题在这里已经有了答案: Firebase kicks out current user (19 个回答) 关闭 3 年前。 我使用带有 firebase 的 Angular 应用程序,并且我具
是否可以在 Firebase 中使用附加字段创建用户?在我的代码中,我可以使用“电子邮件”和“密码”创建它们。 this.af.auth.createUser({ name: formDa
我的 Angular 项目成功使用了 AngularFireAuth可从 @angular/fire 注入(inject)连接到本地 Firebase 身份验证模拟器。我使用这些资源作为我的实现的引用
我已更改官方 AngularJS 文档以使用 Firebase(手机教程)。 这是我的应用程序的路由器: angular.module('phonecat', ['firebase']). conf
我正在使用 Ionic4 和 @angular/fire 编写聊天应用程序代码,但在运行我的应用程序时出现白页和错误消息 (F12)。有人可以帮助我吗? 谢谢。 应用程序模块.ts import {
如何获取登录用户? 我正在这样做: console.log('正在登录...'); this.afAuth.auth.signInWithEmailAndPassword(this.email, th
亲爱的 stackoverflow 社区, 我有以下问题。我尝试将 FirebaseAuthentication 与 Angular7 一起使用,并尝试使用守卫保护路由,因此只有登录用户才能访问 /p
我有一个核心服务,它获取 firebase.auth obj 的实例并使用 getter 返回它。它很简单,如下所示: export class AuthService { ... const
我已经使用 AngularFire2 使用 jasmine/karma 为我的 Angular2 Web 应用程序创建了测试用例,但遇到了一个奇怪的错误,我无法调试。当我运行测试(检查应用程序组件是否
我有一个核心服务,它获取 firebase.auth obj 的实例并使用 getter 返回它。它很简单,如下所示: export class AuthService { ... const
我有一个 authService实例化时订阅 AngularFireAuth的可观察 authState并设置服务的内部(私有(private))属性authState . 所以我可以单元测试auth
有人知道 Angularfire 的新代码 - Angular 4 signInWithEmailAndPassword 方法吗? loginWithEmail(email: string, pass
我已经使用 Firebase 身份验证和 Cloud Fire Store 建立了一个新的 Angular 6 项目。有一个登录页面,您可以在其中通过谷歌登录,用户数据保存在 Firestore 中(
我正在使用 Angular 和 firebase(使用 AngularFire2)构建一个应用程序。尝试在任何 firebase 可观察对象(FirebaseAuthObservable、Fireba
抱歉,我想不出更好的方法来包含所有信息...当我运行它时,我收到一条错误消息,内容如下。我已经按照 Ionic Docs 到 T,我无法弄清楚可能出了什么问题。 错误: No provider for
我知道以前曾有人问过这个问题,但我见过的解决方案都不适合我。我使用 firebase-ui 为我的 ionic 应用程序构建一个登录页面,我使用与其他应用程序完全相同的方法,并且它在那里工作。我安装了
我们尝试使用(Firebase/ionic2/angularjs2)通过谷歌身份验证登录。我们的代码 import { Component } from '@angular/core'; impor
我正在尝试将 angularFireAuth(来自 AngularFire - Firebase)与 ui-route 结合使用。 问题是 angularFireAuth 需要 ng-route 作为
我一直在兜圈子,试图对依赖于 AngularFireAuth 的服务 (AuthService) 进行单元测试。 我正在尝试找到一种方法来模拟或劫持 Observable AngularFireAut
我是一名优秀的程序员,十分优秀!