refactor(core): Migrate TestBed.get to TestBed.inject (#32382)

This is cleanup/followup for PR #32200

PR Close #32382
This commit is contained in:
Carlos Ortiz García
2019-08-28 16:22:36 -07:00
committed by Matias Niemelä
parent a64eded521
commit 9166baf709
69 changed files with 507 additions and 485 deletions

View File

@ -28,9 +28,9 @@ describe('HeroesService', () => {
// Inject the http, test controller, and service-under-test
// as they will be referenced by each test.
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
heroService = TestBed.get(HeroesService);
httpClient = TestBed.inject(HttpClient);
httpTestingController = TestBed.inject(HttpTestingController);
heroService = TestBed.inject(HeroesService);
});
afterEach(() => {
@ -44,7 +44,7 @@ describe('HeroesService', () => {
let expectedHeroes: Hero[];
beforeEach(() => {
heroService = TestBed.get(HeroesService);
heroService = TestBed.inject(HeroesService);
expectedHeroes = [
{ id: 1, name: 'A' },
{ id: 2, name: 'B' },

View File

@ -27,8 +27,8 @@ describe('HttpClient testing', () => {
});
// Inject the http service and test controller for each test
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
httpClient = TestBed.inject(HttpClient);
httpTestingController = TestBed.inject(HttpTestingController);
});
// #enddocregion setup
// #docregion afterEach

View File

@ -6,7 +6,7 @@ describe('MyLibService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: MyLibService = TestBed.get(MyLibService);
const service: MyLibService = TestBed.inject(MyLibService);
expect(service).toBeTruthy();
});
});

View File

@ -103,7 +103,7 @@ xdescribe('AppComponent & Lazy Loading (not working yet)', () => {
beforeEach(fakeAsync(() => {
createComponent();
loader = TestBed.get(NgModuleFactoryLoader);
loader = TestBed.inject(NgModuleFactoryLoader);
loader.stubbedModules = { expected: HeroModule };
router.resetConfig([{path: 'heroes', loadChildren: 'expected'}]);
}));

View File

@ -46,21 +46,21 @@ describe('demo (with TestBed):', () => {
beforeEach(() => {
TestBed.configureTestingModule({ providers: [ValueService] });
// #enddocregion value-service-before-each
service = TestBed.get(ValueService);
service = TestBed.inject(ValueService);
// #docregion value-service-before-each
});
// #enddocregion value-service-before-each, value-service-inject-before-each
// #docregion value-service-inject-it
it('should use ValueService', () => {
service = TestBed.get(ValueService);
service = TestBed.inject(ValueService);
expect(service.getValue()).toBe('real value');
});
// #enddocregion value-service-inject-it
it('can inject a default value when service is not provided', () => {
// #docregion testbed-get-w-null
service = TestBed.get(NotProvided, null); // service is null
service = TestBed.inject(NotProvided, null); // service is null
// #enddocregion testbed-get-w-null
});
@ -109,8 +109,8 @@ describe('demo (with TestBed):', () => {
]
});
// Inject both the service-to-test and its (spy) dependency
masterService = TestBed.get(MasterService);
valueServiceSpy = TestBed.get(ValueService);
masterService = TestBed.inject(MasterService);
valueServiceSpy = TestBed.inject(ValueService);
});
// #enddocregion master-service-before-each

View File

@ -65,9 +65,9 @@ describe('HeroesService (with mocks)', () => {
// Inject the http, test controller, and service-under-test
// as they will be referenced by each test.
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
heroService = TestBed.get(HeroService);
httpClient = TestBed.inject(HttpClient);
httpTestingController = TestBed.inject(HttpTestingController);
heroService = TestBed.inject(HeroService);
});
afterEach(() => {
@ -80,7 +80,7 @@ describe('HeroesService (with mocks)', () => {
let expectedHeroes: Hero[];
beforeEach(() => {
heroService = TestBed.get(HeroService);
heroService = TestBed.inject(HeroService);
expectedHeroes = [
{ id: 1, name: 'A' },
{ id: 2, name: 'B' },

View File

@ -27,8 +27,8 @@ describe('HttpClient testing', () => {
});
// Inject the http service and test controller for each test
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
httpClient = TestBed.inject(HttpClient);
httpTestingController = TestBed.inject(HttpTestingController);
});
// #enddocregion setup
// #docregion afterEach

View File

@ -25,8 +25,8 @@ describe('WelcomeComponent (class only)', () => {
]
});
// inject both the component and the dependent service.
comp = TestBed.get(WelcomeComponent);
userService = TestBed.get(UserService);
comp = TestBed.inject(WelcomeComponent);
userService = TestBed.inject(UserService);
});
// #enddocregion class-only-before-each
@ -93,7 +93,7 @@ describe('WelcomeComponent', () => {
// #docregion setup
// #docregion inject-from-testbed
// UserService from the root injector
userService = TestBed.get(UserService);
userService = TestBed.inject(UserService);
// #enddocregion inject-from-testbed
// get the "welcome" element by CSS selector (e.g., by class name)

View File

@ -1031,7 +1031,7 @@ the setup of the _service-under-test_.
Now requests made in the course of your tests will hit the testing backend instead of the normal backend.
This setup also calls `TestBed.get()` to inject the `HttpClient` service and the mocking controller
This setup also calls `TestBed.inject()` to inject the `HttpClient` service and the mocking controller
so they can be referenced during the tests.
### Expecting and answering requests

View File

@ -355,7 +355,12 @@ array of the services that you'll test or mock.
header="app/demo/demo.testbed.spec.ts (provide ValueService in beforeEach">
</code-example>
Then inject it inside a test by calling `TestBed.get()` with the service class as the argument.
Then inject it inside a test by calling `TestBed.inject()` with the service class as the argument.
**Note:** We used to have `TestBed.get()` instead of `TestBed.inject()`.
The `get` method wasn't type safe, it always returned `any`, and this is error prone.
We decided to migrate to a new function instead of updating the existing one given
the large scale use that would have an immense amount of breaking changes.
<code-example
path="testing/src/app/demo/demo.testbed.spec.ts"
@ -1063,14 +1068,14 @@ The component injector is a property of the fixture's `DebugElement`.
{@a testbed-get}
#### _TestBed.get()_
#### _TestBed.inject()_
You _may_ also be able to get the service from the root injector via `TestBed.get()`.
You _may_ also be able to get the service from the root injector via `TestBed.inject()`.
This is easier to remember and less verbose.
But it only works when Angular injects the component with the service instance in the test's root injector.
In this test suite, the _only_ provider of `UserService` is the root testing module,
so it is safe to call `TestBed.get()` as follows:
so it is safe to call `TestBed.inject()` as follows:
<code-example
path="testing/src/app/welcome/welcome.component.spec.ts"
@ -1080,7 +1085,7 @@ so it is safe to call `TestBed.get()` as follows:
<div class="alert is-helpful">
For a use case in which `TestBed.get()` does not work,
For a use case in which `TestBed.inject()` does not work,
see the [_Override component providers_](#component-override) section that
explains when and why you must get the service from the component's injector instead.
@ -1102,7 +1107,7 @@ a clone of the provided `userServiceStub`.
#### Final setup and tests
Here's the complete `beforeEach()`, using `TestBed.get()`:
Here's the complete `beforeEach()`, using `TestBed.inject()`:
<code-example path="testing/src/app/welcome/welcome.component.spec.ts" region="setup" header="app/welcome/welcome.component.spec.ts"></code-example>
@ -3090,13 +3095,13 @@ Here are the most important static methods, in order of likely utility.
What if the service is optional?
The `TestBed.get()` method takes an optional second parameter,
The `TestBed.inject()` method takes an optional second parameter,
the object to return if Angular can't find the provider
(`null` in this example):
<code-example path="testing/src/app/demo/demo.testbed.spec.ts" region="testbed-get-w-null" header="app/demo/demo.testbed.spec.ts"></code-example>
After calling `get`, the `TestBed` configuration is frozen for the duration of the current spec.
After calling `TestBed.inject`, the `TestBed` configuration is frozen for the duration of the current spec.
</td>
</tr>