1

I have a DataService and I want to assert that the year is getting set in the query string correctly. Is there a way to spyOn the http.get call or to access it? I don't know the correct approach to testing this. I'm using Angular 2.2.0.

The DataService

    constructor(private http: Http) { }
    public getEnergyData(option: string): Promise<EnergyDataDto[]> {
        return this.http.get(this.getEnergyDataApiUrl(option)).toPromise().then((response) => {
            this.energyDataCache = this.parseEnergyDataResponse(response);
            return this.energyDataCache;
        }).catch(this.handleError);
    }

    protected getEnergyDataApiUrl(option: string) {
        return `/api/solar?year=${option}`;
    }

    protected parseEnergyDataResponse(response: Response) {
        return response.json().data;
    }

dataservice.spec.ts

describe('Given the DataService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpModule],
      providers: [DataService, { provide: XHRBackend, useClass: MockBackend }],
    });
  });
  describe('When getting the energy data', () => {
    let backend: MockBackend;
    let service: EnergyDataService;
    let fakeEnergyData: EnergyDataDto[];
    let response: Response;

    const makeEnergyData = () => {
      let data = [];
      let one = new EnergyDataDto();
      one.year = 2007;
      one.countryName = 'Denmark';
      one.quantity = '100000';
      data.push(one);
      return data;
    };
    beforeEach(inject([Http, XHRBackend], (http: Http, be: MockBackend) => {
      backend = be;
      service = new EnergyDataService(http);
      fakeEnergyData = makeEnergyData();
      let options = new ResponseOptions({ status: 200, body: { data: fakeEnergyData } });
      response = new Response(options);
    }));
    it('should return fake values', async(inject([], () => {
      backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));
      service.getEnergyData('all').then(data => {
        expect(data.length).toBe(1);
        expect(data[0].countryName).toBe('Denmark');
      });
    })));

    it('should use year in query string', async(inject([], () => {
      spyOn(service, 'getEnergyDataApiUrl').and.callThrough();
      backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));
      service.getEnergyData('2007').then(data => {
        // I was hoping to use backendend somehow instead, but it's not in scope when I debug it.
        expect((<any>service).getEnergyDataApiUrl).toHaveBeenCalledWith('/api/solar?year=2007');
      });
    })));

1 Answer 1

1

You should do this in the mockBackend.connections subscription. This is when you have access to the URL from the MockConnection

backend.connections.subscribe((c: MockConnection) => {
  expect(c.request.url).toBe(...)
  c.mockRespond(response)
});
Sign up to request clarification or add additional context in comments.

Comments

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.