使用Typescript进行集成测试不会将回调调用到订阅

时间:2018-04-26 14:41:23

标签: angular typescript integration-testing

我正在编写集成测试来检查后端的API。

服务是:

export class Service {
    check(details: Credential, successBlock, errorBlock){
        let localString = "changeAuthenticationCredentialAPI/v2/authenticationCredential/check";


        let url = BASE_URL_SERVER + localString;
        let body = { details };
        let success = (data) => { 

            let _authorization = data.headers["Authorization"];
            let _clientID = data.body.relatedParty[0].id[0].value;
            let _token : Token = {};
            _token.token = _authorization;
            _token.clientId = _clientID;


            successBlock(_token);
        }

        this.post(success,errorBlock,url,body);
    }
    
    handleErrorSync(error) {
        try {
            return Observable.throw(JSON.parse(error._body));
        } catch (e) {
            return Observable.throw(error._body);
        }
    }

    
    post(success, error, url, body) {
        const options = {
            headers: new HttpHeaders(HEADER_REQUEST)
        };

        let call = this.http.post(url, body,options).catch(this.handleErrorSync);
        this.makeCall(call, success, error);
    }

    makeCall(call, success, error) {
        call.flatMap(
            (result) => {

                if (this.responseIsOk(result)) {

                    return Observable.of(result);
                } else {

                    error(result);
                    return Observable.throw(result);
                }
            }
        ).subscribe(
            (result) => {

                 success(result); },
            (err) => {

                this.popupError(err);
                error(err);
            }
        );
    }
 }

关于集成测试看起来像

describe("authentication-credential-service.spec", () => {
    let backend : Service


    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                HttpClientModule,
                HttpClientTestingModule
            ],
            providers: [
                Service,
                { provide: ToastController, useClass: class { ToastController = jasmine.createSpy("ToastController"); } },
                { provide: ModalController, useClass: class { ModalController = jasmine.createSpy("ModalController"); } },
                FileTransfer,
                File
            ]
        });

        backend = TestBed.get(Service);
    }));



    it("should be created", inject([Service], (service: Service) => {
        expect(service).toBeTruthy();
    }));


    describe("check", () => {
       it( "should answer 200 with the right token", async(inject([Service], (service:Service) => {
            let credential: Credential = {}; 
            credential.credentialType = "password";
            credential.usernameText = "user";
            credential.passwordText = "therightpassword";
            let _result : Token 
            let successBlock = ( (data) => { 
                _result = data
                expect(data).toBeDefined();
            } );
            let errorBlock = (error) => {
                expect(false).toBeTruthy();
            };
            service.check(
                credential,
                successBlock,
                errorBlock
            );
        })));
    });

})

问题是运行测试从不调用订阅到“makeCall”方法。所以基本上测试通过但仅仅因为没有运行测试。

使用它的主要目的是检查与我后端的其余api的一致性但在那里我发现只有使用模拟后端或模拟http响应的测试:完美的单元测试但不适用于集成测试。

还有一件事:当然调试它运行的代码并调用subscribe。

有任何线索吗?感谢

1 个答案:

答案 0 :(得分:0)

最后我发现问题与我使用HttpClientModule和HttpClientTestingModule进入testBed配置的导入这一事实有关,并且不知何故,这会破坏集成测试。 删除HttpClientTestingModule就可以了解

describe("authentication-credential-service.spec", () => {
let backend : Service


beforeEach(async(() => {
    TestBed.configureTestingModule({
        imports: [
            HttpClientModule
        ],
        providers: [
            Service,
            { provide: ToastController, useClass: class { ToastController = jasmine.createSpy("ToastController"); } },
            { provide: ModalController, useClass: class { ModalController = jasmine.createSpy("ModalController"); } },
            FileTransfer,
            File
        ]
    });

    backend = TestBed.get(Service);
}));



it("should be created", inject([Service], (service: Service) => {
    expect(service).toBeTruthy();
}));


describe("check", () => {
   it( "should answer 200 with the right token", async(inject([Service], (service:Service) => {
        let credential: Credential = {}; 
        credential.credentialType = "password";
        credential.usernameText = "user";
        credential.passwordText = "therightpassword";
        let _result : Token 
        let successBlock = ( (data) => { 
            _result = data
            expect(data).toBeDefined();
        } );
        let errorBlock = (error) => {
            expect(false).toBeTruthy();
        };
        service.check(
            credential,
            successBlock,
            errorBlock
        );
    })));
});
})