@@ -7,13 +7,11 @@ test development by automatically mocking class external dependencies.
7
7
#### Introduction
8
8
9
9
The dependency injection container is an essential component of the Nest module system.
10
- This container is utilized both during the testing phase and the application runtime.
11
-
10
+ This container is utilized both during the testing phase and the application runtime. \
12
11
Unit tests vary from other types of tests, such as integration tests, in that they must
13
12
fully override providers/services within the DI container. External dependencies (providers)
14
13
of the so-called "unit" should be totally isolated. That is, all dependencies within
15
- the DI container should be replaced by mock objects.
16
-
14
+ the DI container should be replaced by mock objects. \
17
15
As a result, loading the modules and replacing the providers inside them is a process that
18
16
loops back on itself. Automock tackles this issue by automatically mocking all the
19
17
external dependencies/providers, resulting in total isolation of the unit/class
@@ -31,9 +29,7 @@ Automock does not require any additional setup.
31
29
Sinon will shortly be released.
32
30
33
31
#### Example
34
-
35
32
Consider the following cats service, which takes three constructor parameters:
36
-
37
33
``` ts
38
34
@@filename (cats .service )
39
35
import { Injectable } from ' @nestjs/core' ;
@@ -96,7 +92,6 @@ describe('Cats Service Unit Spec', () => {
96
92
> info ** info** The jest.Mocked<Source > utility type returns the Source type
97
93
> wrapped with type definitions of Jest mock function. ([ reference] ( https://jestjs.io/docs/mock-function-api/#jestmockedsource ) )
98
94
99
- \
100
95
#### About ` unit ` and ` unitRef `
101
96
Let's examine the following code:
102
97
@@ -106,14 +101,12 @@ const { unit, unitRef } = Spec.create(CatsService).compile();
106
101
107
102
Calling ` .compile() ` returns an object with two properties, ` unit ` , and ` unitRef ` .
108
103
109
- ` unitRef ` is a small container which holds the class external dependencies (that
104
+ ** ` unitRef ` ** is a small container which holds the class external dependencies (that
110
105
has been replaced with mocks).
111
106
It has one method, ` get() ` , which returns the mocked dependency, thus,
112
107
it enables all the stubbing options from Jest.
113
108
114
- > info ** info** unitRef.get() takes a string (token) or a class (dependency)
115
-
116
- ` unit ` is the actual unit under test, it's an instance of the tested class.
109
+ ** ` unit ` ** is the actual unit under test, it's an instance of the tested class.
117
110
118
111
#### Handling Different Scenarios
119
112
Nest offers different ways to retrieve dependencies from the DI container:
0 commit comments