Mock 服务中的方法被调用,但 toHaveBeenCalled() 在 Angular Jasmine 中失败
Method in Mock service is called but the toHaveBeenCalled() fails in Angular Jasmine
我正在尝试测试模拟服务方法是否被 component.When 中的方法调用 我使用
console.log() 方法内部 logOut() [服务内部方法] 它打印在控制台中,但是
测试失败
header.component.ts
中的方法
onLogOut()
{
this.authenticationService.logOut();
}
文件header.component.spec.ts
@Injectable()
class MockAuthService extends AuthenticationService {
user$ = of(new User('R-234', 'Alex', '23@gmail.com'));
logOut() {
console.log('Logged Out');
}
}
描述('HeaderComponent', () => {
let component: HeaderComponent;
let authService: AuthenticationService;
let fixture: ComponentFixture<HeaderComponent>;
beforeEach(async () => {
TestBed.configureTestingModule({
declarations: [HeaderComponent],
imports: [HttpClientTestingModule, RouterTestingModule],
providers: [HttpClient, AuthenticationService, MockAuthService],
}).compileComponents();
TestBed.overrideComponent(HeaderComponent, {
set: {
providers: [
{ provide: AuthenticationService, useClass: MockAuthService },
],
},
});
});
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
authService = TestBed.get(AuthenticationService);
});
it('should call logOut method once in the service', fakeAsync(() => {
let spy = spyOn(authService, 'logOut').and.callThrough();
component.onLogOut();
expect(spy).toHaveBeenCalledTimes(1);//this fails
}));
});
尝试去掉 overrideComponent
,我认为这可能会导致获取 AuthenticationService
的正确句柄的问题。
您应该直接在 TestBed
中 useClass
进行模拟。
let component: HeaderComponent;
let authService: AuthenticationService;
let fixture: ComponentFixture<HeaderComponent>;
beforeEach(async () => {
TestBed.configureTestingModule({
declarations: [HeaderComponent],
imports: [HttpClientTestingModule, RouterTestingModule],
providers: [HttpClient, AuthenticationService,
// add this line
{ provide: AuthenticationService, useClass: MockAuthService }
],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
authService = TestBed.get(AuthenticationService);
});
it('should call logOut method once in the service', fakeAsync(() => {
let spy = spyOn(authService, 'logOut').and.callThrough();
component.onLogOut();
expect(spy).toHaveBeenCalledTimes(1);//this fails
}));
});
我正在尝试测试模拟服务方法是否被 component.When 中的方法调用 我使用 console.log() 方法内部 logOut() [服务内部方法] 它打印在控制台中,但是 测试失败
header.component.ts
中的方法onLogOut()
{
this.authenticationService.logOut();
}
文件header.component.spec.ts
@Injectable()
class MockAuthService extends AuthenticationService {
user$ = of(new User('R-234', 'Alex', '23@gmail.com'));
logOut() {
console.log('Logged Out');
}
}
描述('HeaderComponent', () => {
let component: HeaderComponent;
let authService: AuthenticationService;
let fixture: ComponentFixture<HeaderComponent>;
beforeEach(async () => {
TestBed.configureTestingModule({
declarations: [HeaderComponent],
imports: [HttpClientTestingModule, RouterTestingModule],
providers: [HttpClient, AuthenticationService, MockAuthService],
}).compileComponents();
TestBed.overrideComponent(HeaderComponent, {
set: {
providers: [
{ provide: AuthenticationService, useClass: MockAuthService },
],
},
});
});
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
authService = TestBed.get(AuthenticationService);
});
it('should call logOut method once in the service', fakeAsync(() => {
let spy = spyOn(authService, 'logOut').and.callThrough();
component.onLogOut();
expect(spy).toHaveBeenCalledTimes(1);//this fails
}));
});
尝试去掉 overrideComponent
,我认为这可能会导致获取 AuthenticationService
的正确句柄的问题。
您应该直接在 TestBed
中 useClass
进行模拟。
let component: HeaderComponent;
let authService: AuthenticationService;
let fixture: ComponentFixture<HeaderComponent>;
beforeEach(async () => {
TestBed.configureTestingModule({
declarations: [HeaderComponent],
imports: [HttpClientTestingModule, RouterTestingModule],
providers: [HttpClient, AuthenticationService,
// add this line
{ provide: AuthenticationService, useClass: MockAuthService }
],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
authService = TestBed.get(AuthenticationService);
});
it('should call logOut method once in the service', fakeAsync(() => {
let spy = spyOn(authService, 'logOut').and.callThrough();
component.onLogOut();
expect(spy).toHaveBeenCalledTimes(1);//this fails
}));
});