在Nodejs項目里寫測試

ayli3691 8年前發布 | 9K 次閱讀 Node.js Node.js 開發

來自: http://giscafer.com/2016/02/12/nodejs-mocha-test/

不寫測試的項目都是耍流氓

</div>

BDD和TDD的差別: The Difference Between TDD and BDD

測試框架:

mocha

Mocha是一個基于node.js和瀏覽器的集合各種特性的Javascript測試框架,并且可以讓異步測試也變的簡單和> 有趣。Mocha的測試是連續的,在正確的測試條件中遇到未捕獲的異常時,會給出靈活且準確的報告。

輔助工具

  • should.js (BDD)
  • chai(支持assert,should,expect)
  • supertest (接口測試,代替瀏覽器地址請求,十分方便)

一個典型的mocha例子:

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


describe('Test', function(){
    before(function() {
    // runs before all tests in this block
    });
  after(function(){
    // runs after all tests in this block
  });
  beforeEach(function(){
    // runs before each test in this block
  });
  afterEach(function(){
    // runs after each test in this block
  });

  describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      [1,2,3].indexOf(5).should.equal(-1);
      [1,2,3].indexOf(0).should.equal(-1);
    });
  });
});
})

</div> </div>

 本文由用戶 ayli3691 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!