undefined 不是服务存根 [Jasmine] 中可观察响应中的对象



这是我第一次尝试在 Angular 中为返回 Observable 的服务编写测试。我收到一个错误,说"this.hubConnector.authenticate((.do">未定义。

我不明白发生了什么。 Observable.of 不足以提供 Observable 响应(也尝试使用 Observable.create 并手动创建 Observable(?我应该改用间谍和.returnValue吗?我是否错误地注入了服务或依赖项?

对我的任何见解都有助于我了解问题所在,以及进行此测试的最佳方法将不胜感激。在过去的两个小时里,我已经搜索了这个网站,我希望这不是一个重复的问题。

我大部分时间都在遵循本指南:https://angular.io/guide/testing
角度 5.0.0
茉莉花 2.6.2
业力 1.7.0

服务。

import { Injectable } from '@angular/core';  
import { HubConnectorComponent } from 'angl-spawebbgrl/hub-connector-
component/hub-connector';  
import { Observable } from 'rxjs/Observable';  
import { SSOUser } from '../../shared/models/sso.model';  
import { EncriptionService } from 'angl-spawebbgrl/encription';  
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class AuthService {
private _usuario: SSOUser = null;
constructor(private hubConnector: HubConnectorComponent, private encriptionService: EncriptionService) {}
autenticar(): Promise<any> {
    this._usuario = null;
    return this.hubConnector
        .authenticate()
        .do((res: any) => {
            this._usuario = res;
            this.encriptionService.changeKeys();
            this.encriptionService.encrypt('yqb').subscribe((docCriptografado: string) => {});
        })
        .toPromise()
        .catch((err: any) => Promise.resolve());
}
    get usuario(): SSOUser {
        return this._usuario;
    }
}

测试。

import { AuthService } from './auth.service';  
import { SSOUser } from '../models/sso.model';  
import { ComponentFixture, TestBed, async, fakeAsync, tick } from
'@angular/core/testing';  
import { EncriptionService } from 'angl-spawebbgrl/encription';  
import { HubConnectorComponent } from 'angl-spawebbgrl/hub-connector-component/hub-connector';  
import { HubConnectorModule } from 'angl-spawebbgrl/spa-http-module/hub-connector.module';  
import { Observable } from 'rxjs/Observable';  
import 'rxjs/Rx';  
describe('AuthService', () => {
let authService: AuthService;
let encriptionService: EncriptionService;
let hubConnectorComponent: HubConnectorComponent;
beforeEach(() => {
    const hubConnectorComponentStub = {
        authenticate(): Observable<any> {
            return Observable.of(<SSOUser>{ user: 'X123456', id: '123456' });
        }
    };
    const encriptionServiceStub = {
        changeKeys(): void {},
        encrypt(valor: string): Observable<string> {
            return Observable.of('BZvebaV07yxksbkg1G4YWUGJuXlh39qThCTEPRQOLE8WnjH6JMGm7DdwQJkImRRF9kB728a/JG');
        }
    };
    TestBed.configureTestingModule({
        imports: [],
        declarations: [],
        providers: [
            AuthService,
            { provide: HubConnectorComponent, useValue: hubConnectorComponentStub },
            { provide: EncriptionService, useValue: encriptionServiceStub }
        ]
    });
    authService = TestBed.get(AuthService);
    encriptionService = TestBed.get(EncriptionService);
    hubConnectorComponent = TestBed.get(HubConnectorComponent);
});
it('test', fakeAsync(() => {
    spyOn(hubConnectorComponent, 'authenticate');
    console.log(authService);
    console.log(hubConnectorComponent);
    authService.autenticar();
    tick();
    expect(hubConnectorComponent.authenticate).toHaveBeenCalled();
}));
});

业力错误 .

LOG: AuthService{hubConnector: Object{authenticate: function () { ... }}, encriptionService: Object{changeKeys: function () { ... }, encrypt: function (valor) { ... }}, _usuario: null}
LOG: Object{authenticate: function () { ... }}
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 7 of 8 SUCCESS (0 secs / 3.569 secs)
PhantomJS 2.1.1 (Mac OS X 0.0.0) AuthService test FAILED
    TypeError: undefined is not an object (evaluating 'this.hubConnector
                .authenticate()
                .do') in http://local.jigsaw.dev.corp:9877/_karma_webpack_/main.bundle.js (line 157836)

在测试中删除 spyOn(hubConnectorComponent, 'authenticate'(;

最新更新