FREELessons: 67Length: 8.9 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

8.1 Service Unit Test Preparation

In this lesson we'll get ready to test our app. I'll explain some of the preliminary steps that we need to complete in order to get the existing spec files working correctly so that we can write our own unit tests.

1.Introduction
6 lessons, 42:00

1.1
Introduction
00:48

1.2
Get Started With Angular-CLI
11:09

1.3
Developing With Angular-CLI
13:17

1.4
TypeScript vs. JavaScript
06:54

1.5
Angular Modules From the CLI
04:31

1.6
CLI Options
05:21

2.Get Started With Angular
7 lessons, 42:38

2.1
Bootstrapping the Application
04:30

2.2
The Application Module
04:15

2.3
The Application Component
08:06

2.4
Component Styling
03:06

2.5
Global Styling
05:11

2.6
Creating a Component With the CLI
09:34

2.7
Creating a Service With the CLI
07:56

3.Core Concepts
7 lessons, 55:20

3.1
Component Trees
06:20

3.2
Dependency Injection
06:52

3.3
Content Projection
05:38

3.4
Component and Directive Lifecycle Methods
06:31

3.5
Component-Only Lifecycle Methods
05:28

3.6
Decorators
07:36

3.7
Models
16:55

4.Template Deep Dive
11 lessons, 1:10:56

4.1
Basic Data Binding With Interpolation
05:35

4.2
Property Bindings
07:07

4.3
Attribute Bindings
03:29

4.4
Event Bindings
08:16

4.5
Class and Style Bindings
05:44

4.6
The `NgClass` and `NgStyle` Directives
05:04

4.7
The `*ngIf` Directive
04:41

4.8
The `*ngFor` Directive
09:29

4.9
Inputs
05:33

4.10
Using Pipes in a Template
07:31

4.11
Using Pipes in a Class
08:27

5.Forms
10 lessons, 1:45:41

5.1
Handling User Input With Template Reference Variables
07:06

5.2
Template-Driven Forms
11:10

5.3
Template-Driven Forms: Validation and Submission
14:00

5.4
Reactive Forms
11:26

5.5
Using a `FormBuilder`
08:01

5.6
Reactive Validation With Built-in Validators
14:53

5.7
Creating Custom Validators for Template-Driven Forms
12:18

5.8
Creating Custom Validators for Reactive Forms
08:26

5.9
Observing Form State Changes
12:40

5.10
Working With the `@HostListener` Decorator
05:41

6.Routing
9 lessons, 1:15:10

6.1
Defining and Configuring Routes
07:53

6.2
Rendering Components With Router Outlets
10:14

6.3
Using Router Links for Navigation
05:25

6.4
Navigating Routes Using the Router
06:24

6.5
Determining the Active Route Using an Activated Route
07:16

6.6
Working With Route Parameters
10:42

6.7
Using Route Guards
07:36

6.8
Observing Router Events
10:55

6.9
Adding Child Routes
08:45

7.Using the HTTP Client
5 lessons, 56:24

7.1
Sending an HTTP Request
10:52

7.2
Handling an HTTP Response
11:22

7.3
Setting Request Headers
12:33

7.4
Intercepting Requests
09:04

7.5
Finishing the Example Application
12:33

8.Testing
10 lessons, 1:23:27

8.1
Service Unit Test Preparation
10:45

8.2
Unit Testing Services
13:24

8.3
Component Unit Test Preparation
12:35

8.4
Unit Testing Components
07:27

8.5
Unit Testing Component Templates
06:58

8.6
Unit Testing Pipes
04:41

8.7
Unit Testing Directives
04:56

8.8
Unit Testing Validators
04:48

8.9
Unit Testing Observables
11:37

8.10
Unit Testing HTTP Interceptors
06:16

9.Building for Production
1 lesson, 03:40

9.1
Building for Production
03:40

10.Conclusion
1 lesson, 01:32

10.1
Conclusion
01:32


8.1 Service Unit Test Preparation

Hi folks, in this lesson we're going to take a look at a unitest that the Angular CLI creaed for us when we first created the service that the test is for. And we'll see what changes we need to make just to get the test to actually run. As we've been working through the example part of the course, we've been creating components, services, and all of the other pieces of our application. And as we've been doing that, the CLI has also been creating spec files for most of these things for us, but the problem is that none of the tests will currently run because our components all have dependencies. And the test will need to be updated to include these dependencies. So there are some preliminary steps that we'll need to take just to get the tests to run, and that's what we're going to look at in this lesson. So we're gonna start out by testing the services. So before we get started, let's just try to run the tests and see what happens. So the test command will build a test version of our application and it will open a browser to try to run the tests, but as you can see, we'll just be left looking at a blank white screen. We don't even have any console errors. So we'll start out by testing the services first. The first service in the folder is the constant service, but that's pretty much just a bunch of strings and there's no functionality, and hence nothing to test. So let's delete the spec file for the constant service. We aren't gonna be using that at all. Next up is the game service. That does have some functionality, so we'd better write some tests for it. And let's just open the service being tested and the spec file. Let's just arrange the editor so that they're both onscreen at the same time. Great, so in the spec file we can see that there is some tests boiler plate ready that the CLI added for us when we created this service originally. So let's just stop the test command running because it will keep trying to run the tests and popping this browser window open. So the example test that is in the service at the moment is pretty rubbish in general because Angular is responsible for creating the service. So this test is just testing that Angular itself is working correctly. I'm pretty sure that Angular has its own set of unit tests to test whether or not it's able to create services. We can delete this example test shortly but it's not entirely pointless. We can use it to get the text file setup correctly because at the moment, even this test won't run. So, let's just focus on this test, we can do that by changing the L to describe to be an F describe. And let's try running the tests once again. And let's see if there's any console messages this time. And it's not even getting far enough to add some console messages. So even though there weren't any errors in the console, we can see that there was an error in the command line here, and the error is that the module app.component can't be found in the home.component.spec file. Now we have added a focus to the game service spec file. So it's just gonna run that one test, but in order to run that one test karma still has to build our application and it currently can't do that, because this module app.component no longer exists. So the first thing that we need to do is to fix that. So let's open up the spec file for the home component. And we don't want to import the app component we want to import the home component And we considered there's a few other places. And actually it's not in the folder called home because we're already in that folder. And we can see there's a few other instances of app components through out this file so let's just correct those. So we fixed all the issues there let's see if we can run the test again. So we can see that the test runner did manage to actually do something this time. But we can see that there are still some errors. We're seeing a static injector error, and the message is that there is no provider for the player service. And what this means is that, let's just go back to the editor. So the game service imports a number of things and one of the things that it imports is the player service. So we're going to need to inject that player service into our test. So we know that an Angular application is made up of one or more modules. And that our components and services all hang off of that module. A test is slightly different. Angular constructs a special module just for testing. And the thing that we are testing and any of its dependencies are injected into this special test module. So this error means that there's no provider for the dependant service. No problem at all, we just need to register the player service with a provider so that it can be injected into the test module. So at the top of the test you can see there is this configure testing module method of the test bed class which is used to coordinate unit tests in Angular. We can see that this has a provider's array and that's where the game service, which is the service that we actually want to test, we can see that that is being injected currently. So, we should just import the player service and we can inject that along with the game service. So now let's go back to the browser. And we can see that the error has updated, and it's telling us a very similar thing, that this time, it can't find a provider for the http client, okay. So, let's go back to the code again. And now we're going to need to import the http client. So http is a bit more complex than our single service. For testing purposes there is actually a whole http client testing module that we can import. And that just takes care of a bunch of things so that we don't have to import and stub all of the http things that we want to do. So we can't add this to the providers array because that's only for services and this is a module. But we can add an imports array just like an ng module has. And we can import the httpClientTestingModule in that way. So again, as soon as we save the file, the tests should be run again. So let's go back to the browser and see if anything is happening. Success. We can see that the test is now running. There's only a single test and that is the generic should be created test but at least we know that everything is working as it should do now. So we can now delete the almost pointless test and write some tests of our own. There's just one bit of the example test that we want to keep though. So, can you see how the game service is injected down in the it method? The inject method takes a number of injection tokens within an array as the first argument, and then we can pass those injection tokens into a callback passed as the second argument. This is the test equivalent of dependency injection. So we do need to inject the game service because we want to test it, but we don't have to repeat this injection in every single test in the file. There could be many, many it methods throughout the file and we don't want to keep having to inject the game service. So let's just add another before each after the first one and capture the injected service in a variable that we can then reuse throughout the test file. And we don't want to actually make. And we don't want to actually make an assertion here, so let's just store the service in the game service variable that we added to the top. So the game service is being injected here and you can see from the parameter passed to the arrow function, that the local reference to the service is just service. So all we do is save that reference in our own variable game service. And then we should be able to use that throughout the rest of the file. So let's just get rid of that it method now. And now we're all set to start writing our own tests for this service. And we'll start doing that in the next lesson. So in this lesson, we looked at some of the things that we need to fix just to get our tests running. Each of the automatically generated spec files will need this kind of treatment. Bringing independencies and building up the test module. So it's essential to know how to get through this process as quickly as possible. Thanks for watching.

Back to the top