On Github rhettl / but-how-do-i-test-really
this is me. I am not in many places yet and I haven't been on the scene long, but I have been working hard and learning much.
These reasons are all crap really
More often then not we all know HOW to test on a theoretical sense. This is similar to how I as an engineer have a theoretical working knowledge of the internal workings of the combustion engine. That said, I cannot fix a combustion engine nor would I know the first thing about where to start or really how to build one from scratch.
Most of us work in multiple environments with multiple different testing requirements and unless we already know how to do tests in all of those environments we wont do testing at all -- or at least on the lenguages we don't know how to test on.
We usually don't give thees answers because we know how bad it makes us look as programmers. When we talk to other programmers, whether they test or not, we feel guilty that we don't test and we know that we should be testing even though we aren't.
Don't feel guilty, Just try testing. It really is not that difficult and it isn't hard to work into your workflow.
JavaScript used to be a bitch to work with as little as 5 years ago. It was a pain impossible to truly write cross-env code.
Internet Explorer is evil. It is everywhere and supports NOTHING that is common to other browsers
I'll explain more about most of these in future slides.
var Calc = function(start) { this.total = start || 0; this.equals = function() { /* ... */ }; this.clear = function() { /* ... */ }; this.add = function() { /*... */ }; this.subtract = function() { /*... */ }; this.multiply = function() { /*... */ }; this.divide = function() { /*... */ }; this.less = this.minus = this.subtract; this.greaterThan = this.plus = this.add; this.and = this.by = this.times = this.multiply; this.over = this.dividedBy = this.divide; };
If you want to test your code, AngularJS or not, and you want half the work done form you, you will need Jasmine (or at least some testing framework). Frameworks are the best way to do QUICK tests that give you UNDERSTANDABLE fail messages
If you know that your code is ONLY going to run in one version of one browser, and you plan to run your tests through github or want to invent some procedure for WHEN you run your tests you may. Karma knows all these things for you. It does them all easily. Just tell it WHEN to test (ex: every save or before every commit) and what to test on and it will handle everything else. No need to waste time deciding when to run it and opening each browser individually, just run it ALL THE TIME and test in ALL browsers (or most)
Karma sits on Node.js. Is there any question why it is awesome!
In short... <Next slide>
var Calc = function(start) { this.total = start || 0; this.equals = function() { /* ... */ }; this.clear = function() { /* ... */ }; this.add = function() { /*... */ }; this.subtract = function() { /*... */ }; this.multiply = function() { /*... */ }; this.divide = function() { /*... */ }; this.less = this.minus = this.subtract; this.greaterThan = this.plus = this.add; this.and = this.by = this.times = this.multiply; this.over = this.dividedBy = this.divide; };
var calcA = new Calc(5) .plus(6).greaterThan(10).add(1, 3) .less(10).minus(1, 2) .by(30).and(10) .over(3).dividedBy(16); console.log(calcA.equals()); // equals 75
var calcA = new Calc(5) .plus(6).greaterThan(10).add([1, 3]) .less(10).minus([1, 2]) .by(30).and(10) .over(3).dividedBy(16); console.log(calcA.equals()); // equals NaN
describe('A Number', function(){ var number = 0; it('should equal 0 in the beginning', function(){ expect(number).toEqual(0); }); it('should be 5 after adding 5', function(){ expect(number + 5).toEqual(5); }); it('should not equal 10 when adding 3', function(){ expect(number + 3).not.toEqual(10); }); //it('should equal 10 when adding 3', function(){ // expect(number + 3).toEqual(10); // This will fail //}); });
describe('A Calc', function(){ var calc; beforeEach(function(){ calc = new Calc(0); }); it('should add 2 numbers', function(){ expect(calc.plus(2).plus(10).equals()).toEqual(12); }); it('should subtract 2 numbers', function(){ expect(calc.plus(2).minus(10).equals()).toEqual(-8); }); it('should multiply 2 numbers', function(){ expect(calc.plus(2).times(10).equals()).toEqual(20); }); it('should divide 2 numbers', function(){ expect(calc.plus(2).over(10).equals()).toEqual(0.2); }); it('should accept multiple args', function(){ expect(calc.plus(2, 10).equals()).toEqual(12); }); });
<link rel="stylesheet" type="text/css" href="...jasmine.css"> <script type="text/javascript" src="...jasmine.js"></script> <script type="text/javascript" src="...jasmine-html.js"></script> <script type="text/javascript" src="...boot.js"></script> <!-- include source files here... --> <script type="text/javascript" src="...Calc.js"></script> <!-- include spec files here... --> <script type="text/javascript" src="...CalcSpec.js"></script>
var Calc = function(start) { this.total = start || 0; this.equals = function() { /* ... */ }; this.clear = function() { /* ... */ }; this.add = function() { /*... */ }; this.subtract = function() { /*... */ }; this.multiply = function() { /*... */ }; this.divide = function() { /*... */ }; this.less = this.minus = this.subtract; this.greaterThan = this.plus = this.add; this.and = this.by = this.times = this.multiply; this.over = this.dividedBy = this.divide; };
$ npm install karma --save-dev $ npm install karma-jasmine karma-chrome-launcher --save-dev $ ./node_modules/karma/bin/karma start INFO [karma]: Karma v0.12.3 server started at http://localhost:9876/ $ npm install -g karma-cliKarma Installation
This was taken almost directly from the Karma website
$ karma init my.conf.js Which testing framework do you want to use ? Press tab to list possible options. Enter to move to the next question. > jasmine Do you want to use Require.js ? This will add Require.js plugin. Press tab to list possible options. Enter to move to the next question. > no Do you want to capture a browser automatically ? Press tab to list possible options. Enter empty string to move to the next question. > Chrome > Firefox > What is the location of your source and test files ? You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js". Enter empty string to move to the next question. > *.js > test/**/*.js > Should any of the files included by the previous patterns be excluded ? You can use glob patterns, eg. "**/*.swp". Enter empty string to move to the next question. > Do you want Karma to watch all the files and run the tests on change ? Press tab to list possible options. > yes Config file generated at "/Users/vojta/Code/karma/my.conf.js". $ karma start my.conf.jsKarma Configuration
Karma can also write the file in coffee fi you prefer
I have yet to figure our how to run Internet explorer on Ubuntu in a way that is worth the amount of effort required, but I think you must have the browser on your computer to make it work. I was going to ask Rit Lee tonight, but he was called away and is not here.
describe('A Calc', function(){ var calc; beforeEach(function(){ calc = new Calc(0); }); /* ... Previous Tests ... */ it('should res-et back to 0 when res-et', function(){ expect(calc.plus(1,2,3).times(10).clear().equals()).toEqual(0); }); it('should set the total to any number with set()', function(){ expect(calc.set(100).equals()).toEqual(100); }); });
var Calc = function(start) { this.total = start || 0; this.equals = function() { /* ... */ }; this.clear = function() { /* ... */ }; this.set = function() { /* ... */ }; this.add = function() { /*... */ }; this.subtract = function() { /*... */ }; this.multiply = function() { /*... */ }; this.divide = function() { /*... */ }; this.less = this.minus = this.subtract; this.greaterThan = this.plus = this.add; this.and = this.by = this.times = this.multiply; this.over = this.dividedBy = this.divide; };
Writing Code just to pass tests !== Good Code !== User Friendly
Overall: Remember the user is not a computer