Skip to content

HttpClient testing guide suggests using deprecated TestBed.get invocation on abstract class HttpTestingController #30750

Closed

Description

@textbook

📚 Docs or angular.io bug report

Description

The example showing how to set up tests for a component consuming the HttpClient shows the following usage:

httpTestingController = TestBed.get(HttpTestingController);

This is deprecated from v8.0.0 (because the HttpTestingController is an abstract class), and therefore throws a warning in the linter; the docs should be updated to use (or at least show an example of) the recommended, compliant code.

🔬 Minimal Reproduction

What's the affected URL?**

https://angular.io/guide/http#setup-1

Reproduction Steps**

Add the minimal HttpClient testing example from the documentation:

describe('HttpClient testing', () => {
  let httpClient: HttpClient;
  let httpTestingController: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [ HttpClientTestingModule ]
    });

    // Inject the http service and test controller for each test
    httpClient = TestBed.get(HttpClient);
    httpTestingController = TestBed.get(HttpTestingController);
  });
  /// Tests begin ///
});

Run the linter:

$ ng lint
Linting "demo"...

...
WARNING: /path/to/app/service.test.ts:16:41 - get is deprecated: from v8.0.0 use Type<T> or InjectionToken<T>

Lint warnings found in the listed files.

Expected vs Actual Behavior**

Expected: docs example uses up-to-date code
Actual: docs example uses deprecated code, leading to a warning

📷Screenshot

🔥 Exception or Error


WARNING: /path/to/app/service.test.ts:16:41 - get is deprecated: from v8.0.0 use Type or InjectionToken

🌍 Your Environment

Browser info

Anything else relevant?

Activity

  1. trotyl commented on May 30, 2019

    @trotyl
    Contributor

    Duplicate of #29905


    Note: the deprecation is not ideal and has been temporarily reverted disabled.

  2. textbook commented on May 30, 2019

    @textbook
    ContributorAuthor

    @trotyl I wasn't sure whether it would be a duplicate or not - it's not clear to me from the other issue whether TestBed.get(HttpTestingController) is still going to be the right way to get the controller, or whether the method will require additional type information. At the very least, I'd like to track here that the docs get some kind of update in the interim.

  3. trotyl commented on May 30, 2019

    @trotyl
    Contributor

    whether TestBed.get(HttpTestingController) is still going to be the right way to get the controller

    It is the right way, any class is valid injection token, previously discussed a lot at #25222 and #26491.

  4. textbook commented on May 30, 2019

    @textbook
    ContributorAuthor

    @trotyl cool, thanks for the info

  5. angular-automatic-lock-bot commented on Sep 15, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  6. locked and limited conversation to collaborators on Sep 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions