I have an Angular 2 service connected to the Angular Http service with Rxjs 5 to connect to a restful web service. The getObjects call simply returns the parsed Json in the form of an Observable array of meaningful objects. I've been trying to get the observable returned to resolve with my mocked Http response, but I haven't really found a working answer to this.
Test code:
import { getTestBed } from '@angular/core/testing';
import { MockBackend } from '@angular/http/testing';
import { TestScheduler } from "rxjs";
import { expect } from 'chai';
import { spy } from 'sinon';
import TestingUtilities from "../shared/test.utilities";
import Service from './service';
import ReturnObject from "../returnobject";
describe(`ServiceTests`, () => {
let MOCK_DATA: string = ...mocked JSON string response...;
let service: Service
let backend: MockBackend
let scheduler: TestScheduler
function assertDeepEqualFrame(actual:any, expected:any) {
console.log("test");
if (!expected === actual) {
throw new Error('Frames not equal!');
}
}
beforeEach(() => {
TestingUtilities.configureTestingModuleForMockHttp(getTestBed(), function () {
return Service
});
backend = getTestBed().get(MockBackend);
service = getTestBed().get(EarthquakeService);
scheduler = new TestScheduler(assertDeepEqualFrame);
});
it('should return mocked data', () => {
TestingUtilities.mockHttpResponse(backend, MOCK_DATA);
let observables = service.getObjects();
scheduler.expectObservable(observables).toBe("", functionToCreateMockObjects());
});
The TestingUtilities is just a convenience wrapper around the solution for mocking the Http service provided by Angular presented at https://semaphoreci.com/community/tutorials/testing-angular-2-http-services-with-jasmine. This code above compiles, but it doesn't actually seem to return the mocked Observables nor assert on anything. I'm struggling to see exactly how the TestScheduler should be used to call an existing service and get observables back for validation. Anyone have any ideas?