Angular 2: MockBackend returns "undefined" response

I try to write unit test for my service using MockBackend, and every time I get the answer as undefined. Any help would be greatly appreciated. I have tested all these solutions below and compared to mine, I really do not see any big differences.

Here is my service:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { SomeObject } from './someObject';

@Injectable()
export class SomeService {

    private serviceUrl: string = 'http://localhost:8080/getObjects';  

    constructor( private http: Http ) { }

    getObjects() {
        return this.http.get( this.serviceUrl )
            .map(( response ) => response.json().content as SomeObject[])
    }
}

Here are my tests:

import { Http, BaseRequestOptions, Response, ResponseOptions } from '@angular/http';
import { TestBed, tick, fakeAsync, inject } from '@angular/core/testing';
import { MockBackend } from '@angular/http/testing';
import { SomeService } from './some.service';
import { SomeObject } from './someObject';

describe( 'SomeServiceTest', () => {

    let stubData: SomeObject[] = [
        new SomeObject( 1, "Title" )
    ];

    beforeEach(() => {
        TestBed.configureTestingModule( {
            providers: [
                SomeService,
                MockBackend,
                BaseRequestOptions,
                {
                    provide: Http,
                    useFactory: ( backend: MockBackend, options: BaseRequestOptions ) => {
                        return new Http( backend, options );
                    },
                    deps: [MockBackend, BaseRequestOptions]
                }
            ]
        });
    });

    it( 'should fetch data', inject( [SomeService, MockBackend], fakeAsync(( service: SomeService, mockBackend: MockBackend ) => {
        let res: SomeObject[];
        mockBackend.connections.subscribe( c => {
            expect( c.request.url ).toBe( 'http://localhost:8080/getObjects' );
            c.mockRespond( new Response( new ResponseOptions( {
                body: JSON.stringify( stubData )
            }) ) );
        });

        service.getObjects().subscribe(( response ) => {
            console.log( "response :: " + JSON.stringify( response ) ); // this is where I am seeing undefined
            res = response;
        });

        tick();

        expect( res[0].name ).toBe( 'Title' ); // this returns an exception TypeError: Cannot read property '0' of undefined
    }) ) );
});
+2
source share
1 answer

, , , , response.json().content as Object[] service.the content, , undefined. json .

getObjects() {
    return this.http.get( this.dealsUrl )
        .map(( response ) => response.json());
}

service.getSomeObjectsUsingObservable().subscribe(( response ) => {
    console.log( "response :: " + JSON.stringify( response ) );
    if (response.content){
        res = response as SomeObject[];
    }
    else {
        //- fail test
    }
});
tick();
expect( res[0].name ).toBe( 'Title' );

,

+4

Source: https://habr.com/ru/post/1675094/


All Articles