NG2 RC5: HTTP_PROVIDERS is deprecated
Asked Answered
W

2

13

So, in version RC5 of Angular2, they deprecated the HTTP_PROVIDERS and introduced the HttpModule. For my application code, this is working fine, but I'm struggling to make the change in my Jasmine tests.

Here's what I'm currently doing in my specs, but since HTTP_PROVIDERS is deprecated, what should I be doing now? Is there something I need to provide instead of HTTP_PROVIDERS? What is the correct way to do this in the RC5 world?

beforeEach(() => {
  reflectiveInjector = ReflectiveInjector.resolveAndCreate([
    HTTP_PROVIDERS,
    ...
  ]);

  //other code here...
});

it("should....", () => { ... });
Wellintentioned answered 11/8, 2016 at 18:23 Comment(7)
HTTP_PROVIDERS have been deprecated and replaced by HttpModule .Ardine
Great, that part I get. Now, how do you use this in a Jasmine test? :)Wellintentioned
I am having the same exact issue, even opened a bug as this was a major feature and I don't see how it can now be used... :(Ultrastructure
@Ardine thanks for that useful information. I wonder if there is a generic way of finding what replaces a deprecated type as far as angular 2 is concerned. How did you find out about the HttpModule yourself for instance?Heatstroke
@balteo... You can google for 'angular2 breaking changes'. Best of luck. I cant send you a link as sending this text from cellphone.Ardine
@Heatstroke Yeah its frustrating that they often deprectate stuff but don't point to what should replace the deprecated component. You have to do a ton of digging to figure it out. But I guess it's still in RC, so we can't be too whiney :) They will often release a breaking changes doc when they do new releases, and outline that info but I'm not sure that's always the case...Centring
"BREAKING CHANGE: previously deprecated HTTP_PROVIDERS and JSONP_PROVIDERS were removed; see deprecation notice for migration instructions." Yes, but where do I find the deprecation notice??Eighteenth
H
11

The now deprecated HTTP_PROVIDERS is replaced with the HttpModule is RC5.

Within your describe block, add the TestBed.configureTestingModule with the requisite imports and providers array attributes like below:

describe("test description", () => {
    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [HttpModule],
            providers: [SomeService]
        });
    });
    it("expect something..", () => {
        // some expectation here
        ...
    })
})

This is how I got my unit service tests to work with RC5, hopefully this won't have to change between the next release candidates and the final version. If you are like me, you are probably frustrated with the amount of deprecation that is going on between release candidates. I hope things stabilize soon!

Haematoxylon answered 25/8, 2016 at 15:49 Comment(1)
Do import { HttpModule } from '@angular/http';.Stride
E
2

I run into a similar problem when updating from pre-RC5 code to RC6. To expand on Joe W's answer above, I replaced this code:

import { ReflectiveInjector, provide } from '@angular/core';
import { HTTP_PROVIDERS, RequestOptions } from '@angular/http';

export function main() {
  describe('My Test', () => {
    let myService: MyService;

    beforeAll(() => {
      let injector = ReflectiveInjector.resolveAndCreate([
        HTTP_PROVIDERS,
        provide(RequestOptions, { useValue: getRequestOptions() }),
        MyService
      ]);
      myService = injector.get(MyService);
    });

    it('should be instantiated by the injector', () => {
      expect(myService).toBeDefined();
    });
...

with this RC6 code (which, I guess, should also work for RC5):

import { TestBed } from '@angular/core/testing';
import { HttpModule, RequestOptions } from '@angular/http';

export function main() {
  describe('My Test', () => {
    let myService: MyService;

    beforeAll(() => {
      TestBed.configureTestingModule({
        imports: [HttpModule],
        providers: [
          { provide: RequestOptions, useValue: getRequestOptions() },
          MyService
        ]
      });
      myService = TestBed.get(MyService);
    });

    it('should be instantiated by the testbed', () => {
      expect(myService).toBeDefined();
    });
...
Eolande answered 12/9, 2016 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.