Slide 1

Slide 1 text

Unit Testing a modular Backbone Application

Slide 2

Slide 2 text

About Us Me: A humble student of the trade! You: 1. Understand Javascript 2. Understand MVC 3. Want to test your code

Slide 3

Slide 3 text

What are we going to talk about? ● Namespaces ● Modularity ■ AMD ■ A modular Backbone Application ● Unit Testing with QUnit ● Patterns in Testing Client Side MVC

Slide 4

Slide 4 text

No Deep Diving

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

"Let me tell you why I love javascript, it's a thing called stockholm syndrome" - Alex Gaynor ● Namespaces are big issues ● Lacks Maintainability & Patterns

Slide 7

Slide 7 text

Namespaces ● JS does not have a built in support for them ● but Closures can help achieve similar effect ● which leads to Preventing Pollution of Global Scope

Slide 8

Slide 8 text

Namespaces - Prefix namespacing for global variables ● Single Global variables work fine. ● Wisdom lies in avoiding conflict. var myApplication_listView = Backbone.View.extend({});

Slide 9

Slide 9 text

Namespaces - Object Literals ● Assist in organizing code and parameters logically var myApp = myApp || {};

Slide 10

Slide 10 text

Namespaces - Nested Namespacing ● Extension of Object Literal Pattern ● Reduces Chances of Collisions (different parents won't have same children) YAHOO.util.Dom. getElementsByClassName ('namespace');

Slide 11

Slide 11 text

Modularity in a BackBone App highly decoupled, distinct, that enforces maintainability & dependency minimization ● How do I declare & invoke? ● How do I organize my code? ● How do I organize my code? ● What should be my project Structure?

Slide 12

Slide 12 text

AMD with RequireJS

Slide 13

Slide 13 text

The only not so good thing about tests is writing them.

Slide 14

Slide 14 text

About Testing

Slide 15

Slide 15 text

Why Unit, then Integration then ..? ● Code of scale n, will have unit tests of scale n ● Exhaustive Correctness can't be tested ○ BackBone.js has 1477 LOC, with 201 ifs ○ From Basic Combinatorics, for exhaustive testing NTC = 2 ^ 201 = 32138760885179805510839241 84682325205044405987565585 670602752

Slide 16

Slide 16 text

What & When

Slide 17

Slide 17 text

Introducing & Running QUnit

Slide 18

Slide 18 text

Simple Test function isPalin( str ){ return (str.reverse() == str) ? true : false } module( 'Module One' ); test( 'isPalin()', function() { expect( 2 ); equal( isPalin('madam'), 'true', 'Test a true palindrome' ); notEqual( isPalin('world'), 'fasle', 'Test a false palindrome'); });

Slide 19

Slide 19 text

Sample Test module( "Module One", { setup: function() { // run before }, teardown: function() { // run after } }); test("first test", function() { // run the first test }); http://siliconforks.com/jscoverage/

Slide 20

Slide 20 text

QUnit Demo

Slide 21

Slide 21 text

TDD Backbone - Models 1. New instances can be created with the expected default values 2. Attributes can be set and retrieved correctly 3. Changes to state correctly fire off custom events where needed 4. Validation rules are correctly enforced

Slide 22

Slide 22 text

TDD Backbone - Collections 1. New model instances can be added as both objects and arrays 2. Changes to models result in any necessary custom events being fired 3. A url property for defining the URL structure for models is correctly defined

Slide 23

Slide 23 text

TDD Backbone Views 1. They are being correctly tied to a DOM element when created 2. They support wiring up view methods to DOM elements

Slide 24

Slide 24 text

TakeAways ● Namespaces are a great honking ideas, lets do more of those. ● There should be one-- and preferably only one --obvious way to do it. ● Explicit is better than Implicit. ● Now is better than never. ● Although practicality beats purity.

Slide 25

Slide 25 text

Introducing HackBookPro

Slide 26

Slide 26 text

Questions ● Contact: ○ at gamebit07 (twitter) ○ gamebit07 at gmail dot com