Test Driven Development with Protractor/Jasmine (Legacy)
This module is deprecated and no longer receives updates. Protractor is likely being removed as the default from Angular applications and Protractor itself will likely stop receiving updates and development in the future. I would recommend checking out the Test Driven Development with Cypress/Jest as a replacement.
Jasmine, Karma, and Protractor
WARNING: This module is deprecated and no longer receives updates. Protractor is likely being removed as the default from Angular applications and Protractor itself will likely stop receiving updates and development in the future. I would recommend checking out the Test Driven Development with Cypress/Jest as a replacement.
The tools we need to test our applications
DEPRECATEDModule Outline
- Resources PRO
- Lesson 1: Introduction PRO
- Lesson 2: Introduction to Test Driven Development PRO
- Lesson 3: Testing Concepts PRO
- Lesson 4: Jasmine, Karma, and Protractor PRO
- Lesson 5: A Simple Unit Test PRO
- Lesson 6: A Simple E2E Test PRO
- Lesson 7: Introduction to Angular's TestBed PRO
- Lesson 8: Setting up Tests PRO
- Lesson 9: Test Development Cycle PRO
- Lesson 10: Getting Ready PRO
- Lesson 11: The First Tests PRO
- Lesson 12: Injected Dependencies & Spying on Function Calls PRO
- Lesson 13: Building out Core Functionality PRO
- Lesson 14: Testing Asynchronous Code PRO
- Lesson 15: Creating a Mock Backend PRO
- Lesson 16: Setting up the Server PRO
- Lesson 17: Testing Integration with a Server PRO
- Lesson 18: Testing Storage and Reauthentication PRO
- Lesson 19: Refactoring with Confidence PRO
- Lesson 20: Conclusion PRO
Lesson Outline
Jasmine, Karma, and Protractor
We've established that we need to write tests before we write code when developing an application if we are adhering to the principles of Test Driven Development. In the case of unit tests, we could just write plain old Javascript to test if our code is doing what we want it to do, but that would be quite messy and hard to manage.
In this lesson, we are going to discuss the frameworks that we will be using to create unit tests and E2E tests throughout this module.
Creating Unit Tests with Jasmine and Karma
We will be using Jasmine and Karma to unit test our application. Jasmine is what we use to create the unit tests, and Karma is what runs them for us using a browser.
Jasmine is a framework for writing tests. The code you will write with Jasmine is reasonably simple, and is primarily composed of three key functions: describe()
, it()
, and expect()
.
Let's take a look at a Jasmine test suite right away for context.
describe('My Service', () => {
it('should correctly add numbers', () => {
const result = myService.myAddNumbersFunction(1, 1);
expect(result).toBe(2);
});
it('should correctly subtract numbers', () => {
const result = myService.mySubtractNumbersFunction(8, 3);
expect(result).toBe(5);
});
});
As you can see, we are using all three of those functions in the tests above: describe
, it
, and expect
.
The describe()
function defines a suite of tests. A "suite" is just a group of related tests, and tests are also often referred to as "specs". This is typically where we would perform our Arrange step in the AAA process, meaning we will prepare the testing environment for our tests at this point (we will discuss another special function we can use for this in a moment).
The it()
function defines a specific test (or "spec"), and it lives inside of a suite. This function is responsible for defining the purpose of the test (i.e. "it should do this", "it should do that"), and it holds the code for the test. You can add whatever code you like within this it
block to perform the actions necessary for the test. This is the Act step of the AAA process.
The expect()
defines the expected result of a test and lives inside of a specific test. Once you have finished whatever actions are necessary to perform your test, you use expect
to enforce an expectation on something that should have happened in the test. This is the Assert step of the AAA process.
In the example I've used, we have a test suite called My Service. Inside of that suite, we define two tests. The first test defines a test for correctly adding numbers, and it expects that if 1
and 1
are passed into a specific function then the returned result should be 2
. The second test is similar, except that it is testing a subtraction function.
I mentioned that we can use another function in Jasmine to help us arrange our tests. Let's expand our test to include that so it is a little more realistic:
Thanks for checking out the preview of this lesson!
You do not have the appropriate membership to view the full lesson. If you would like full access to this module you can view membership options (or log in if you are already have an appropriate membership).