Jasmine-间谍返回值失败后期待



在更改模拟服务方法返回值后,第二条expect语句出现问题。我试过fakeAsync和Jasmine的done,但仍然失败了,它只适用于我显然想去掉的setTimeout

不工作:

// AppComponent.ts
export class AppComponent implements OnInit {
constructor(private authService: AppAuthService) {}
ngOnInit() {
this.checkAvailableCalculators();
}
private checkAvailableCalculators() {
if (this.authService.isLoggedIn()) {
this.authService.getAvailableCalculators();
}
this.authService.getSessionEvents().subscribe(e => {
if (e === SESSION_EVENTS.login) {
this.authService.getAvailableCalculators();
}
});
}
}
// AppComponent.spec.ts
const authServiceMock = {
getSessionEvents: jasmine.createSpy('getSessionEventsSpy').and.returnValue(of()),
getAvailableCalculators: jasmine.createSpy('getAvailableCalculators')
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [],
providers: [{provide: AppAuthService, useValue: authServiceMock}],
declarations: [AppComponent]
}).compileComponents();
}));
it('should check for available calculators after login', fakeAsync(() => {
component.ngOnInit();
expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();
authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));
tick(1000);
expect(authServiceMock.getAvailableCalculators).toHaveBeenCalled(); // <=== FAILS
}));

工作:


it('should check for available calculators after login', () => {
component.ngOnInit();
expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();
authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));
setTimeout(() => expect(authServiceMock.getAvailableCalculators).toHaveBeenCalled(), 1000); // <=== PASSES
});

我没有看到你的组件代码,但在黑暗中尝试一下:

fixture.whenStable()等待,直到承诺完成。

it('should check for available calculators after login', async(done) => {
component.ngOnInit();
expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();
authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));
await fixture.whenStable();
expect(authServiceMock.getAvailableCalculators).toHaveBeenCalled(); 
done();
});

==============================================编辑=============

现在试试这个:

// AppComponent.spec.ts
const authServiceMock = {
getSessionEvents: jasmine.createSpy('getSessionEventsSpy'),
getAvailableCalculators: jasmine.createSpy('getAvailableCalculators')
};
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [],
providers: [{provide: AppAuthService, useValue: authServiceMock}],
declarations: [AppComponent]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
});
describe('getSessionEvents returning login', () => {
beforeEach(() => {
// mock getSessionEvents before ngOnInit is called
authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));
fixture.detectChanges(); // !! After this fixture.detectChanges, ngOnInit 
will be called, no need to call it explicitly.
});
it('should check for available calculators after login', () => {
expect(authServiceMock.getAvailableCalculators).toHaveBeenCalled(); 
});
});
describe('getSessionEvents returning anything else you want', () => {
beforeEach(() => {
authServiceMock.getSessionEvents.and.returnValue(of('anything else'));
fixture.detectChanges();
});
it('should not call getAvailableCalculators', () => {
expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();
});
});

of运算符可能对您的实现太迟了。of运算符不推送该值,只是在下一个订阅中提供该值。让我知道这是否有效。

相关内容

最新更新