Skip to content

Commit 9a55d1f

Browse files
committed
Introduction to testing
1 parent 8af079d commit 9a55d1f

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Loading
47.5 KB
Loading

js/lesson7/tutorial.md

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
layout: page
3+
title: Introduction to Testing
4+
---
5+
6+
## Today's lesson
7+
8+
### What is testing?
9+
10+
Testing is a way to ensure that the code you have written works correctly. It verifies the quality of your code and makes it easier to identify and fix problems.
11+
12+
Today we will be briefly explaining how you can test-drive your code. Testing is not just a way to ensure that everything works well together, but also means of improving the quality and keeping your code simple and readable.
13+
14+
## Javascript Testing Frameworks
15+
16+
There are [a number](http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript) of libraries written to assist with testing JavaScript, but we will use [Jasmine](http://pivotal.github.io/jasmine/). The syntax is quite simple and it doesn't require any additional configuration in order to use.
17+
18+
## Jasmine
19+
20+
### Syntax
21+
22+
In Jasmine, you can use `describe()` to describe the purpose of a suite of tests, and `it()` to describe a specific test.
23+
24+
25+
```javascript
26+
describe('Calculator', function() {
27+
describe('addition', function() {
28+
it('returns the sum of the numbers', function() {
29+
expect(add(3,5)).toEqual(8);
30+
});
31+
});
32+
});
33+
```
34+
35+
![](assets/images/calculator-test.png)
36+
37+
When a test is no longer working you get a failure
38+
39+
![](assets/images/calculator-test-fail.png)
40+
41+
42+
> Don't forget that you must refresh your browser when you update the tests!
43+
44+
# Exercise 1: Unit Coverter
45+
46+
Using Jasmine to test drive our code, today we will implement a Unit Converter.
47+
48+
To get started, download the code from [our github repository](https://github.com/codebar/TestingJavascript).
49+
50+
For Jasmine to work, it must know about our code and test files. So, before we begin using Jasmine to test, we must update the `SpecRunner.html` to include our files. The code is stored in the `src` directory (which stands for **source**), and our test files in `spec`.
51+
52+
Update the head of the page to include the files.
53+
54+
```html
55+
<!-- include source files here... -->
56+
<script type="text/javascript" src="src/Converter.js"></script>
57+
58+
<!-- include spec files here... -->
59+
<script type="text/javascript" src="spec/ConverterSpec.js"></script>
60+
```
61+
62+
63+
## Converting Farheneit to Celcius
64+
65+
Now, open the SpecRunner in your browser. You will notice that the one test we have is failing.
66+
67+
When we are writing code, it's ok to write a test that is failing before making it work. This way, instead of focusing on getting things to work, we focus on what we want to achieve and then gradually work around that.
68+
69+
We have already added a method that will be handling the conversion of Farheneit to Celcius. We also know that the formula is
70+
71+
```javascript
72+
farheneit = (celcius - 32)/1.8
73+
```
74+
75+
Try implementing the solution to make your test work.
76+
77+
### `toFixed()`
78+
79+
You will notice that this doesn't quite work as we are getting back a number with multiple decimals. We can use the `toFixed(decimal_places)` method to round up the number to one decimal.
80+
81+
```javascript
82+
return farheneit.toFixed(1);
83+
```
84+
85+
> Did you manage to get your test working?
86+
87+
> When you have a test passing, it's the right time to commit your code to git.
88+
89+
> Add another test using a different value for the temperature. Verify the result using google search `X fahrenheit to celsius`
90+
91+
92+
## Converting Celcius to Farheneit
93+
94+
Not that we have that working, let's add another function that converts **celcius** to **farheneit** instead.
95+
First, don't forget to add your test and the expected solution. (You can use google to figure out the result)
96+
97+
To reverse the result we can use this formula:
98+
99+
```javascript
100+
celcius = farheneit * 1.8 + 32
101+
```
102+
103+
## Converting other units
104+
105+
Now, add the tests and implement the conversion between the following units. DOn't forget to commit first, after you write each of your tests and then when you get the test passing.
106+
107+
1. Pounds to kilos (Weight)
108+
```
109+
kilo = pound * 0.4536
110+
```
111+
112+
2. Litre to Gallons (Volume)
113+
````
114+
gallons = litres * 0.22
115+
````
116+
117+
3. Miles to Km (distance)
118+
```
119+
kms = miles * 1.609
120+
```
121+
122+
> Can you think of any other unit conversions you would find handy? Can you implement them?
123+
124+
## Homework
125+
Build an HTML page for your converter.
126+
127+
# Test matchers
128+
129+
Besides the `toEqual()` method, Jasmine has a big set of matchers that we can use to verify the results of our tests like `toBe()`, `not.toBe()`, `toBeNull()` and [a lot others](http://pivotal.github.io/jasmine/)
130+
131+
132+
# Exercise 2: Calculator
133+
134+
Download the [Testing Javascript project again](https://github.com/codebar/TestingJavascript) and create a Calculator.js, under the `/src` directory, and a CalculatorSpec.js under the `/spec` directory. Don't forget to update the SpecRunner so it includes you tests and code files.
135+
136+
1. Write a simple calculator that adds, subtracts, divides and multiplies two numbers together.
137+
2. Commit when you add a test, and after you make each test working.
138+
3. Create a styled website for your calculator
139+
140+
141+
# Additional Resources
142+
143+
You should now understand enough about JavaScript to create and test your own code.
144+
To gain a deeper understanding of JavaScript, we recommend reading [Javascript, the good parts](http://www.amazon.co.uk/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742), by Douglas Crockford.
145+
146+
---
147+
This ends our **Introduction to Testing** tutorial. Is there something you don't understand? Try and go through the provided resources with your coach. If you have any feedback, or can think of ways to improve this tutorial [send us an email](mailto:feedback@codebar.io) and let us know.

0 commit comments

Comments
 (0)