1

I want to write some tests for my service written in Angular 2.1.0:

@Injectable()
export class MyService {
    api_base_url = 'http://' + environment.apiHost + ':5000/api/';

    constructor(private http: Http) {
    }

    getMyObjs(query): Observable<MyObj[]> {
        let params = new URLSearchParams();

        if (query != null && query.length > 0)
            params.set('q', query);

        return this.http.get(this.api_base_url + 'entries/' + pipelineId, {search: params})
            .map(this.extractData)
            .catch(this.handleError);
    }

    private extractData(res: Response) {
        let body = res.json();
        return body.entries || {};
    }

    private handleError(error: any) {
        // In a real world app, we might use a remote logging infrastructure
        // We'd also dig deeper into the error to get a better message
        let errMsg = (error.message) ? error.message :
            error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
    }

}

And here is the test so far:

describe('Service: Entry', () => {

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

}));

it('should return myobjs', inject([MockBackend, MyService], (backend: MockBackend, service: MyService) => {
    const my_body = JSON.stringify([
        {
            "timestamp": "2016.01.01T12:55:00Z",
            "level": 0,
        }
    ]);
    const baseResponse = new Response(new ResponseOptions({body: my_body, status: 200}))
    backend.connections.subscribe((connection: MockConnection) => {
        return connection.mockRespond(baseResponse);
    });

    service.getMyObjs(null).subscribe((obs: MyObj[]) => {
        console.log(obs);
        expect(obs.length).toBe(1);
    });

}));
});

This test fails saying Expected 0 to be 1.. The console.log shows LOG: function obs() { ... }. I do not undestand why this is a function and not an array of objects. I'm runnign the tests with ng test command.

1 Answer 1

1

In the mocked HTTP response you return object structure that is different from what extractData() is expecting. I suppose it should look like this:

    // ... stuff ...
it('should return myobjs', ...) => {
    const my_body = JSON.stringify({
        entries: [{
            "timestamp": "2016.01.01T12:55:00Z",
            "level": 0,
        }]
    });
Sign up to request clarification or add additional context in comments.

1 Comment

You just save my day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.