Writing Tests

In notebook:
FrontEndMasters BackboneJS
Created at:
2016-06-11
Updated:
2016-06-11
Tags:
JavaScript jQuery libraries
A basic test skeleton file (test that the app exists):
  define(function(require, exports, module){
  var assert = chai.assert;
  var App = require('app');
  
  describe('Application View', function(){
    it('it exists', function(){
      assert.ok(false);
    })
  })
})
Talks about reducing side-effects and running tests in isolation.
Running servers (CI, complicated) or even using LocalStorage which is very slow. We need to get around these.

Be careful to not to test things that already have tests written for them, such as Backbone or Backbone.localstorage. 
No reason to test jQuery either (extremely well tested).
Test creating a persona model
He usually tests methods. He writes first the test for the method he wants to create. The goes red-green-refactor.
  define(function(require, exports, module){
  var assert = chai.assert;
  var App = require('app');
  
  describe('Application View', function(){..})
  
  describe('.initialize', function(){
    it('creates a persona model', function(){
      var app =  new App();
      assert.ok(app.persona);
    });
  })
})
He doesn’t always uses full TDD on the front-end. He first finds a solution, then finds a way to test it.The above test fails. Not all dependencies are included. He includes them quickly and it passes.Then does the same for the comments collection. Asserts ​app.collection​.

Then, instead of creating ​new App()​ in each ​it​ block, he adds a ​before​ statement:
  ..
  describe('.initialize', function(){
    before(function(){
      this.app = new App();
    });
    it('creates a persona model', function(){ 
        assert.ok(this.app.persona);
    });
  })
..
Then introduces the ​context​ block. Groups everything into this context block.
  ..
  context('creates dependencies', function(){
    describe('.initialize', function(){
      before(function(){
        this.app = new App();
      });
      it('creates a persona model', function(){ 
          assert.ok(this.app.persona);
      });
    })
  });
..