29
Getting started with Testing in Javascript @Spyr1014

Test driven programming beginner quick start

Embed Size (px)

Citation preview

Page 1: Test driven programming beginner quick start

Getting started with Testing in Javascript

@Spyr1014

Page 2: Test driven programming beginner quick start

Motivation!

• Why should I test?

• How should I test?

• Testing as motivation or HATE DRIVEN DEVELOPMENT

Page 3: Test driven programming beginner quick start

Why?

Page 4: Test driven programming beginner quick start

But I’ve just started…

Page 5: Test driven programming beginner quick start
Page 6: Test driven programming beginner quick start

Get the number 6 from pressing the button 6

Page 7: Test driven programming beginner quick start

‘Title Case’ FCC Problem

• Return the provided string with the first letter of each word capitalised.

• Make sure the rest of the word is in lower case.

https://www.freecodecamp.com/challenges/title-case-a-sentence

Page 8: Test driven programming beginner quick start

Starting codefunction titleCase(str) { return str;}

titleCase("I'm a little tea pot");

// Result we want: "I'm A Little Tea Pot"

Page 9: Test driven programming beginner quick start

FCC test cases

Page 10: Test driven programming beginner quick start

We can set this up ourselves!

Page 11: Test driven programming beginner quick start

Testing Helper Functions!

Page 12: Test driven programming beginner quick start
Page 13: Test driven programming beginner quick start
Page 14: Test driven programming beginner quick start
Page 15: Test driven programming beginner quick start

What do I need to know to test?

Page 16: Test driven programming beginner quick start

Mocha• Testing framework

• Does lots of work for you

Page 17: Test driven programming beginner quick start

Using Mocha• ‘describe(string, function)’

• Group tests and creates heading

• `it(string, function)`

• Your ‘individual test’

Page 18: Test driven programming beginner quick start

describe("Heading 1", function(){ describe("Heading 2", function(){ it("A test", function(){ }); });});

Page 19: Test driven programming beginner quick start

Assertion Library?• Gives you an easy way to check if things are behaving as expected.

• Basically it’s the same as == or ===

I imagine it as ================

Page 20: Test driven programming beginner quick start

Chai - Assertion Library// From http://chaijs.com/

var expect = chai.expect;

expect(name).to.be.a('string');

expect(name).to.equal('Andrew');

expect(foo).to.have.lengthOf(3);

expect(tea).to.have.property('flavours') .with.lengthOf(3);

Page 21: Test driven programming beginner quick start

expect(titleCase("I'm a little tea pot")).to.be.a("string");

expect(titleCase("I'm a little tea pot")) .to.equal("I'm A Little Tea Pot");

expect(titleCase("sHoRt AnD sToUt")) .to.equal("Short And Stout");

expect(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT")) .to.equal("Here Is My Handle Here Is My Spout");

Page 22: Test driven programming beginner quick start

Add `describe` and `it`

Page 23: Test driven programming beginner quick start
Page 24: Test driven programming beginner quick start

The Mocha webpage

Page 25: Test driven programming beginner quick start

Head • Import any JS dependencies

• Mocha style sheet (mocha.css)

• Import Chai

Body • Div with id = “mocha”

• Setup Mocha

• Your code, and testing code.

• Run Mocha

Page 26: Test driven programming beginner quick start

<!DOCTYPE html> <html> <head> <title>Mocha</title> <script src="vendor/chai.js"></script> <link rel="stylesheet" href="mocha.css" /> </head> <body> <div id="mocha"></div> <script src="mocha.js"></script> <script>mocha.setup('bdd');</script> <!--Put your code here--> <script src="problem.js"></script> <script src="problem.test.js"></script> <!--End of your code.--> <script> mocha.run(); </script> </body> </html>

Page 27: Test driven programming beginner quick start

Files set up for you!

https://github.com/SpyR1014/Simple-Mocha-Testing-

Boilerplate

http://blog.sewmucheasier.com/wp-content/uploads/2013/12/surprised-puppy.jpg

Page 28: Test driven programming beginner quick start

RED - GREEN - REFACTOR

• while(problem !== ‘solved’) {

• 1. Write a failing test

• 2. Fix the failing test

• 3. Make the code nice

• }

Page 29: Test driven programming beginner quick start

–Kent Beck (Author of Test Driven Development)

“I'm not a great programmer; I'm just a good programmer with great habits.”