NG2 RC5: HTTP_PROVIDERS is deprecated

放肆的年华 提交于 2019-11-27 17:42:47

问题


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....", () => { ... });

回答1:


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!




回答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();
    });
...


来源:https://stackoverflow.com/questions/38903607/ng2-rc5-http-providers-is-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!