用jasmine-node作測試

方健發表於2015-06-17

安裝:
sudo npm install jasmine-node -g

測試檔案:
vi test/spec/lib/RouteParser.spec.js

'use strict';

describe('RouteParser', function () {
  var RouteParser;

  beforeEach(function () {
    RouteParser = require('../../../src/lib/RouteParser.js');
  });

  it('should retun a []', function () {
    expect(RouteParser).toBeDefined();
    expect(RouteParser.parse).toBeDefined();
    expect(RouteParser.parse()).toEqual([]);
  });

  it('should retun a ["abc"]', function () {
      var path="/abc";
    expect(RouteParser.parse(path)).toEqual([abc]);
  });

});

被測檔案 src/lib/RouteParser.js

var parse=function(routeString){
    //routeString ="#/abb/112/ccd/234/..."
    //return [[abb,112],[ccd,234],...]
    if(!routeString){
        return [];
    }
}

module.exports={
    parse:parse
};

開始測試:

jasmine-node test/spec/

參考:
http://jasmine.github.io/
https://github.com/mhevery/jasmine-node

相關文章