无法在mocked subscribe方法中执行代码

时间:2017-01-31 20:04:05

标签: angular rxjs observable subscribe

在subscribe方法中执行代码。

长话短说我正在测试一个检索JWT token的服务,我需要在subscribe内执行代码,以便在模拟this.jwtService.getToken().subscribe(tokenResp =>时执行,但它不会执行。请参阅subscribe中的评论:

受测试的系统:AuthenticationLibrary.service.ts:

getNewToken() {
    var self = this;
    if (self.busyGettingToken == false) {
        self.busyGettingToken = true;
        this.jwtService.getToken().subscribe(tokenResp => 
        {
             self.token = tokenResp.token<-------CODE NOT MAKING IT INTO HERE
             self.busyGettingToken = false;
             self.frontEndLoggingService.Log("getNewToken() Acquired: " + self.token, true);
        });
    } 
}

isTokenValid() {
    if (this.token == null)
    {
        return false;
    }
    else
    {
        var base64Url = this.token.split('.')[1];
        var base64 = base64Url.replace('-', '+').replace('_', '/');            
        var jwt = JSON.parse(window.atob(base64));            

        if (jwt.exp >= this.configurationService.jwtRenewTime() / 1000) {
            //Does not expire within the next x ms
            return true;
        } else {
            //Expires within the next x ms
            return false;
        }
    }
}  

AuthenticationLibrary.service.spec.ts:

我嘲笑this.jwtService.getToken()部分要返回:

   let jwtSvcSpy = jasmine.createSpyObj('jwtService', ['getToken']);
        jwtSvcSpy.getToken.and.callFake(() => {
            return {
                subscribe: () => {<-------CODE MAKES IT INTO HERE
                    return new Response(
                    new ResponseOptions({
                    body: {
                        token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwuY29tIiwiZXhwIjoxNDI2NDIwODAwLCJodHRwOi8vdG9wdGFsLmNvbS9qd3RfY2xhaW1zL2lzX2FkbWluIjp0cnVlLCJjb21wYW55IjoiVG9wdGFsIiwiYXdlc29tZSI6dHJ1ZX0.yRQYnWzskCZUxPwaQupWkiUzKELZ49eM7oWxAQK_ZXw"
                    }
                    })
                )
                }
            };
        });

以下是整个文件:

/// <reference path="../../typings/globals/es6-shim/index.d.ts"/>
/// <reference path="../../typings/globals/jasmine/index.d.ts" />
import { inject, TestBed, ComponentFixture } from "@angular/core/testing";
import { RouterTestingModule } from "@angular/router/testing";
import { Headers, HttpModule, BaseRequestOptions, XHRBackend, Response, Http, ResponseOptions } from "@angular/http";
import { MockBackend, MockConnection } from "@angular/http/testing";
import { Component, DebugElement } from "@angular/core";
import { By } from '@angular/platform-browser';
import "rxjs/add/operator/toPromise";

import { AuthenticationLibrary } from "../../app/services/authenticationLibrary.service";
import { ConfigurationService } from "../../app/services/configuration.service";
import { FrontEndLoggingService } from "../../app/services/frontEndLogging.service";
import { JwtService } from "../../app/services/jwt.service";
import { UtilitiesService } from "../../app/services/utilities.service";

describe("Authentication service", () => {

    let mockBackend;
    let jwtService;
    let configurationService;
    let http;
    let feLogSvc;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [HttpModule],
            providers: [
                { provide: XHRBackend, useClass: MockBackend },
                ConfigurationService,
                FrontEndLoggingService,
                JwtService,
                UtilitiesService
            ]
        });

        mockBackend = TestBed.get(XHRBackend);
        jwtService = TestBed.get(JwtService);
        configurationService = TestBed.get(ConfigurationService);
        http = new Http(mockBackend, new BaseRequestOptions);
        feLogSvc = TestBed.get(FrontEndLoggingService);
    });

    it("Should tell if token is valid", () => {

        mockBackend.connections.subscribe(
            (connection: MockConnection) => {
                connection.mockRespond(new Response(
                    new ResponseOptions({
                    body: {
                        token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwuY29tIiwiZXhwIjoxNDI2NDIwODAwLCJodHRwOi8vdG9wdGFsLmNvbS9qd3RfY2xhaW1zL2lzX2FkbWluIjp0cnVlLCJjb21wYW55IjoiVG9wdGFsIiwiYXdlc29tZSI6dHJ1ZX0.yRQYnWzskCZUxPwaQupWkiUzKELZ49eM7oWxAQK_ZXw"
                    }
                    })
                ));
            });

   let jwtSvcSpy = jasmine.createSpyObj('jwtService', ['getToken']);
        jwtSvcSpy.getToken.and.callFake(() => {
            return {
                subscribe: () => {
                    return new Response(
                    new ResponseOptions({
                    body: {
                        token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwuY29tIiwiZXhwIjoxNDI2NDIwODAwLCJodHRwOi8vdG9wdGFsLmNvbS9qd3RfY2xhaW1zL2lzX2FkbWluIjp0cnVlLCJjb21wYW55IjoiVG9wdGFsIiwiYXdlc29tZSI6dHJ1ZX0.yRQYnWzskCZUxPwaQupWkiUzKELZ49eM7oWxAQK_ZXw"
                    }
                    })
                )
                }
            };
        });

        let authLibService = new AuthenticationLibrary(
            http,
            configurationService,
            feLogSvc,
            jwtSvcSpy
        );

        authLibService.getNewToken();
        expect(authLibService.isTokenValid()).toBe(true);
    })
});

如何以导致我的顶级代码块中jwtService.getToken方法内的代码执行的方式模拟subscribe

1 个答案:

答案 0 :(得分:2)

您正在返回一个带有subscribe函数的模拟,而不是一个具有订阅函数的模拟器,它接受一个回调 if 10 < int(thenumber[2:4]) < 20: teencheck = "correct" else: teencheck = "wrong" 。我个人会用一个包含你期望的确切数据的版本来存储Observable:

next