Skip to content

Commit 964c386

Browse files
committed
docs: add info about providers, unit and unit-ref
1 parent aa63c96 commit 964c386

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

content/recipes/automock.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class CatsService {
5151
}
5252
```
5353

54-
The service contains one public methods, `getAllCats`, which is the method
54+
The service contains one public method, `getAllCats`, which is the method
5555
we use an example for the following unit test:
5656

5757
```ts
@@ -96,23 +96,27 @@ describe('Cats Service Unit Spec', () => {
9696
Let's examine the following code:
9797

9898
```typescript
99-
const { unit, unitRef } = Spec.create(CatsService).compile();
99+
const { unit, unitRef } = TestBed.create(CatsService).compile();
100100
```
101101

102102
Calling `.compile()` returns an object with two properties, `unit`, and `unitRef`.
103103

104-
**`unitRef`** is a small container which holds the class external dependencies (that
105-
has been replaced with mocks).
106-
It has one method, `get()`, which returns the mocked dependency, thus,
107-
it enables all the stubbing options from Jest.
104+
**`unit`** is the "unit" (service/provider) under test, it is an actual instance of
105+
class being tested (also known as "unit under test").
108106

109-
**`unit`** is the actual unit under test, it's an instance of the tested class.
107+
To store the mocked dependencies of the tested class, the "unit reference" (`unitRef`)
108+
serves as a small container. The container's `.get()` method returns the mocked
109+
dependency with all of its methods automatically stubbed (using `jest.fn()`).
110110

111-
#### Handling Different Scenarios
112-
Nest offers different ways to retrieve dependencies from the DI container:
113-
Actual class, injection tokens, and forwardRef functions. They all can be
114-
replaced with mocks using different techniques, in the next sections you
115-
can understand how to deal with each scenario
111+
The `.get()` method can accept either a `string` or an actual class (`Type`) as its argument.
112+
This essentially depends on how the class dependency is being injected.
113+
Some specific usage scenarios, including when a `string` and when a `Type` are acceptable,
114+
are provided below.
115+
116+
#### Handling Different Injections Types
117+
Providers are one of the most important ideas in Nest. A lot of the basic Nest classes,
118+
such as services, repositories, factories, helpers, and so on, can be treated of as providers.
119+
The main idea behind a provider is that it can be injected as a dependency.
116120

117121
##### Working with Interfaces
118122
Consider the following `CatsService` which takes one param which is an instance

0 commit comments

Comments
 (0)