06 August 2015

Tutorial

Assert engine

준비

mkdir tutorial
cd tutorial
npm init
mkdir app
mkdir test
npm install mocha --save
npm install chai --save

Test Code

test/myTest.js

var expect = require("chai").expect;
var testModule = require("../app/testModule");

describe("testModule should", function() {
    var input = true;
    var expectedResult = true;

    it("do something", function() {
      expect(testModule.doSomething()).to.equal(expectedResult);
    });
});

구현

app/testModule.js

function doSomething(input){
    return true;
}

module.exports.doSomething = doSomething;

테스트 실행

$ mocha

또는

package.json

{
  ...

  "scripts": {
    "test": "./node_modules/.bin/mocha --reporter spec"
  },
  
  ...

  "dependencies": {
    "chai": "^3.2.0",
    "express": "^4.13.3",
    "mocha": "^2.2.5",
    "request": "^2.60.0"
  }
}
$ npm test

테스트 자동 실행 with Gruntfile.js

$ npm install grunt-mocha-test --save-dev

Gruntfile.js

grunt.loadNpmTasks('grunt-mocha-test');

grunt.initConfig({
    watch: {
          mochaTest: {
            files: ['test/**/*.js'],
            tasks: ['mochaTest']
          }
    },      
    mochaTest: {
      options: {
        reporter: 'spec',
        captureFile: 'results.txt',
        quiet: false,
        clearRequireCache: false
      },
      src: ['test/**/*.js']
    }
});

테스트파일 변경시 자동 재실행

$ grunt watch

테스트 실행

$ grunt mochaTest

Node-inspector + Grunt + Mocha

$node-debug $(which grunt) mochaTest

Async Test

테스트 할 때, it 에다가 done 이라는 파라미터를 받으면 done() 하고 실행될때 까지 기다렸다가 테스트 결과를 판단한다.

var expect = require('chai').expect;

describe("async test", function(){
 it("should works!" ,function(done){
   setTimeout(function(){
     console.log("time goes by..");
     expect(true).to.be.true;
     done();
   },2000);
 });
});

테스트가 2초 이상 걸리면 타임아웃 에러가 나므로 다음과 같이 타임 리미트를 늘려주면 된다.

$mocha --timeout 15000

Chai api

  • Chains
    • to
    • be
    • been
    • is
    • that
    • which
    • and
    • has
    • have
    • with
    • at
    • of
    • same
  • not
  • deep
  • any
  • all
  • a / an(type)
  • include / contain / includes / contains (value)
  • ok
  • true
  • false
  • null
  • undefined
  • exist
  • empty
  • arguments / Arguments
  • equal / equals / eq / deep.equal (value) : ===
  • eql / eqls (value) : deep
  • above / gt / greaterThan (value)
  • least / gte (value)
  • below / lt / lessThan (value)
  • most / lte (value)
  • within (start, finish)
  • instanceof / instanceOf (constructor)
  • property / deep.property (name, [value])
  • ownProperty / haveOwnProperty (name)
  • length / lengthOf (value)
  • match (regexp)
  • string (string)
  • keys / key (key1, [key2], […])
  • throw / throws / Throw (constructor)
  • respondTo (method)
  • itself
  • satisfy(method)
  • closeTo(expected, delta) : delta 범위 안에서 근사
  • members (set)
  • change / changes / Change (function)
  • increase / increases / Increase : expect(fn).to.increase(obj, 'val');
  • decrease / decreases / Decrease : expect(fn).to.decrease(obj, 'val');


blog comments powered by Disqus
처음으로