【发布时间】:2017-07-08 11:53:17
【问题描述】:
我正在测试一个显示/隐藏登录/退出按钮的非常简单的组件。
为此,我正在嘲笑我的 AuthService 服务,因为它依赖于 AngularFire2。
我遇到的问题是没有提供我的模拟服务 (Mock AuthService) 来代替实际的 AuthService。
在测试should show the Facebook login button 中,service.isAnonymous 预计是未定义的。在实际服务中,确实如此。但是在模拟服务中是true。这个测试应该失败。
另外,请注意我正在尝试调用方法service.test(false);;在模拟服务中,此方法存在并且是public。但我收到错误:
“AuthService”类型上不存在属性“test”。
这表明没有提供我的模拟服务。
您可以在我的测试规范中看到我如何尝试两种方式来提供模拟服务(其中一种已被注释掉):
import { DebugElement } from '@angular/core';
import {
async,
inject,
ComponentFixture,
TestBed
} from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { FacebookLoginComponent } from './facebook-login.component';
import { AuthService } from '../shared/auth.service';
import { MockAuthService } from '../shared/testing/auth.service';
describe('FacebookLoginComponent', () => {
let authService: AuthService;
let component: FacebookLoginComponent;
let fixture: ComponentFixture<FacebookLoginComponent>;
let debugElement: DebugElement;
let htmlElement: HTMLElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ FacebookLoginComponent ],
// providers: [{ provide: AuthService, useValue: MockAuthService }]
})
.compileComponents();
TestBed.overrideComponent(FacebookLoginComponent, {
set: {
providers: [{ provide: AuthService, useClass: MockAuthService }]
}
})
}));
beforeEach(() => {
fixture = TestBed.createComponent(FacebookLoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should be created', () => {
expect(component).toBeTruthy();
});
it('should show the Facebook login button', inject([ AuthService ], (service: AuthService) => {
expect(service.isAnonymous).toBeUndefined();
debugElement = fixture.debugElement.query(By.css('button'));
htmlElement = debugElement.nativeElement;
service.test(false);
expect(htmlElement.textContent).toBe('Facebook Login');
}));
it('should show the Logout button', () => {
debugElement = fixture.debugElement.query(By.css('button'));
htmlElement = debugElement.nativeElement;
expect(htmlElement.textContent).toBe('Logout');
});
});
为了完整性;这是我的模拟服务,MockAuthService:
import { Injectable } from '@angular/core';
@Injectable()
export class MockAuthService {
public authState: { isAnonymous: boolean, uid: string };
constructor() {
this.authState = { isAnonymous: true, uid: '0HjUd9owxPZ5kibvUCN6S2DgB4x1' };
}
// public get currentUser(): firebase.User {
// return this.authState ? this.authState : undefined;
// }
// public get currentUserObservable(): Observable<firebase.User> {
// return this.afAuth.authState;
// }
public get currentUid(): string {
return this.authState ? this.authState.uid : undefined;
}
public get isAnonymous(): boolean {
return this.authState ? this.authState.isAnonymous : false;
}
public get isAuthenticated(): boolean {
return !!this.authState;
}
// public logout(): void {
// this.afAuth.auth.signOut();
// }
public test(isAnonymous: boolean) {
this.authState.isAnonymous = isAnonymous;
}
}
我不知道如何提供模拟。
更新:
到目前为止,我已经根据答案和 cmets 更新了我的模拟测试规范。但是,我仍然遇到同样的问题。
我收到错误“AuthService”类型上不存在属性“test”。
这表明它仍然没有用模拟代替实际的authService 服务。
此外,当我添加:
public test(test: boolean): boolean {
return test;
}
测试失败的实际服务;但不是因为上面的错误,而是因为它应该——没有满足测试的期望。
这是我更新的规范:
import { DebugElement } from '@angular/core';
import {
async,
inject,
ComponentFixture,
TestBed
} from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { FacebookLoginComponent } from './facebook-login.component';
import { AuthService } from '../shared/auth.service';
import { MockAuthService } from '../shared/testing/auth.service';
describe('FacebookLoginComponent', () => {
let authService: AuthService;
let component: FacebookLoginComponent;
let fixture: ComponentFixture<FacebookLoginComponent>;
let debugElement: DebugElement;
let htmlElement: HTMLElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ FacebookLoginComponent ],
providers: [{ provide: AuthService, useClass: MockAuthService }]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FacebookLoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should be created', () => {
expect(component).toBeTruthy();
});
it('should show the Facebook Login button', inject([ AuthService ], (service: AuthService) => {
debugElement = fixture.debugElement.query(By.css('button'));
htmlElement = debugElement.nativeElement;
expect(htmlElement.textContent).toBe('Facebook Login');
}));
it('should show the Logout button', inject([ AuthService ], (service: AuthService) => {
expect(service.isAnonymous).toBe(true);
debugElement = fixture.debugElement.query(By.css('button'));
htmlElement = debugElement.nativeElement;
service.test(false);
fixture.detectChanges();
expect(htmlElement.textContent).toBe('Logout');
}));
});
【问题讨论】:
-
这是一个类,不是一个值;
useClass: MockAuthService,或useValue: new MockAuthService()。 -
感谢@jonrsharpe,虽然我仍然遇到这个问题,但我已经更新了我的测试规范。请查看我更新的问题。
-
如果你访问
(service as MockAuthService).test会怎样? -
然后我得到错误
Type 'AuthService' cannot be converted to type 'MockAuthService'. Property 'authState' is private in type 'AuthService' but not in type 'MockAuthService'.@jonrsharpe -
检查我更新的答案。
标签: angular unit-testing karma-jasmine