使用http异步进行Angular 5服务单元测试

使用http异步进行Angular 5服务单元测试,angular,Angular,我有以下服务 export class AuthService { constructor(private http: HttpClient) {} login(email: string, password: string) { return this.http.post<any>(environment.url.login, {'username': email, 'password': password}, {}) .map(a

我有以下服务

export class AuthService {
    constructor(private http: HttpClient) {}

    login(email: string, password: string) {
        return this.http.post<any>(environment.url.login, {'username': email, 'password': password}, {})
        .map(async (loginRequest) => {
            // login successful if there's a jwt token in the response
            if (loginRequest && loginRequest.token) {
                localStorage.setItem('token', loginRequest.token);
            }
            return loginRequest.token;
        })
        .pipe(retry(0));
    }
}
结果:预期[object Promise]是ScalarObservable

我在每一个“it(…)”中都看到了使用mock和spy以及注入服务的不同单元测试,我已经阅读了Angular的测试章节,但我不明白这应该如何工作。请帮助我。

第一次测试,尝试
expect(httpClientSpy('post'])。TohaveBeenCall()
。第二次测试,尝试
expect(response)。toEqual(of({token}));
在中解释detail@trichetriche
expect(httpClientSpy['post']).toHaveBeenCalled()
有效,谢谢!
expect(response).toEqual(of({token}));
导致
预期的[对象承诺]等于ScalarObservable[…]
describe('AuthenticationService', () => {
    let httpClientSpy;
    let authenticationService;

    beforeEach(() => {
        // Create a fake HttpClient object with a `post()` spy
        httpClientSpy = jasmine.createSpyObj('HttpClient', ['post']);

        authenticationService = new AuthenticationService(<any> httpClientSpy);

        // bypass localstorage functions to mockLockalStorage
        spyOn(localStorage, 'removeItem')
            .and.callFake(mockLocalStorage.removeItem);
        spyOn(localStorage, 'getItem')
            .and.callFake(mockLocalStorage.getItem);
    });

    it('#login should return observable of token', (done) => {
        httpClientSpy.post.and.returnValue(of({token}));

        const login = authenticationService.login('Username', '123489');
        expect(httpClientSpy).toHaveBeenCalled(); 

        // todo: check return value

        done();
    });
});
    it('#login should return observable of token', (done) => {
        httpClientSpy.post.and.returnValue(of({token}));

        const login = authenticationService.login('Username', '123489');

        login.subscribe(response => {
            expect(response).toBe(of({token}));
        });

        done();
    });
});