最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHPStorm中如何對(duì)nodejs項(xiàng)目進(jìn)行單元測試詳解

 更新時(shí)間:2019年02月28日 15:02:03   作者:悠悠i  
這篇文章主要給大家介紹了關(guān)于PHPStorm中如何對(duì)nodejs項(xiàng)目進(jìn)行單元測試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

安裝必要的包

nodejs的單元測試最常用的是使用mocha包。首先確保你本地安裝nodejs,之后按照mocha包。

npm install mocha -g

然后還需要安裝相關(guān)的斷言工具,Node.js中常用的斷言庫有:

  • assert: TDD風(fēng)格
  • should: BDD風(fēng)格
  • expect: BDD風(fēng)格
  • chai: BDD/TDD風(fēng)格

使用npm install安裝這些斷言庫其中之一即可。

PHPStorm配置nodejs單元測試環(huán)境

在PHPStorm中選擇菜單:Run -> Edit Configurations,點(diǎn)擊右上角添加mocha。


分別填寫下面幾項(xiàng),關(guān)于mocha單元測試可以參考官網(wǎng):https://mochajs.org/

  • Name: 隨便一個(gè)運(yùn)行配置的名稱,如MochaTest
  • Working directory: 當(dāng)前項(xiàng)目目錄
  • Mocha package: Mocha安裝包的目錄,node_modules\mocha
  • User interface: 測試類型,這里選擇TDD(對(duì)應(yīng)assert庫)
  • Test directory: 這一項(xiàng)可以選擇測試目錄或文件
    • All in directory: 整個(gè)目錄都進(jìn)行測試
    • File patterns: 某種模式的文件,可以填正則表達(dá)式
    • Test file: 某個(gè)特定的測試文件

填寫完成并且沒有報(bào)錯(cuò)后點(diǎn)擊OK。

Nodejs進(jìn)行單元測試

這里我們選擇assert庫,TDD模式進(jìn)行單元測試。在上面選定的Test directory目錄下新建一個(gè)測試文件test.js.

const assert = require('assert');

// 測試Array類型的方法
suite('Array', function() {
 // 測試 indexOf方法
 suite('#indexOf()', function() {
  // 測試用例
  test('should return -1 when not present', function() {
   assert.equal(-1, [1, 2, 3].indexOf(4));
  });
 });
});

點(diǎn)擊選擇Mocha運(yùn)行,在PHPStorm下面的輸出框中有測試的結(jié)果,綠色表示通過,紅色表示失敗。

斷言庫的使用

mocha進(jìn)行單元測試的時(shí)候,除了能夠使用assert斷言庫,只要斷言代碼中拋出Error,mocha就可以正常工作。

assert庫:TDD風(fēng)格

下面列舉assert庫中常用的斷言函數(shù),詳情可參考官網(wǎng):https://www.npmjs.com/package/assert

  • assert.fail(actual, expected, message, operator)
  • assert(value, message), assert.ok(value, [message])
  • assert.equal(actual, expected, [message])
  • assert.notEqual(actual, expected, [message])
  • assert.deepEqual(actual, expected, [message])
  • assert.notDeepEqual(actual, expected, [message])
  • assert.strictEqual(actual, expected, [message])
  • assert.notStrictEqual(actual, expected, [message])
  • assert.throws(block, [error], [message])
  • assert.doesNotThrow(block, [message])
  • assert.ifError(value)

其中的參數(shù)說明如下:

  • value: 實(shí)際值
  • actual: 實(shí)際值
  • expected: 期望值
  • block: 語句塊
  • message: 附加信息

BDD風(fēng)格should.js斷言庫

安裝方法:npm install should --save-dev,官網(wǎng)地址:https://github.com/shouldjs/should.js

const should = require('should');

const user = {
 name: 'tj'
 , pets: ['tobi', 'loki', 'jane', 'bandit']
};

user.should.have.property('name', 'tj');
user.should.have.property('pets').with.lengthOf(4);

// If the object was created with Object.create(null)
// then it doesn't inherit `Object.prototype`, so it will not have `.should` getter
// so you can do:
should(user).have.property('name', 'tj');

// also you can test in that way for null's
should(null).not.be.ok();

someAsyncTask(foo, function(err, result){
 should.not.exist(err);
 should.exist(result);
 result.bar.should.equal(foo);
});

should庫可以使用鏈?zhǔn)秸{(diào)用,功能非常強(qiáng)大。相關(guān)文檔參考:http://shouldjs.github.io/

user.should.be.an.instanceOf(Object).and.have.property('name', 'tj');
user.pets.should.be.instanceof(Array).and.have.lengthOf(4);

常用的should斷言方法:

無意義謂詞,沒作用增加可讀性:.an, .of, .a, .and, .be, .have, .with, .is, .which

  • should.equal(actual, expected, [message]): 判斷是否相等
  • should.notEqual(actual, expected, [message]): 判斷是否不相等
  • should.strictEqual(actual, expected, [message]): 判斷是否嚴(yán)格相等
  • should.notStrictEqual(actual, expected, [message]): 判斷是否嚴(yán)格不相等
  • should.deepEqual(actual, expected, [message]): 判斷是否遞歸相等
  • should.notDeepEqual(actual, expected, [message]): 判斷是否遞歸不想等
  • should.throws(block, [error], [message]): 判斷是否拋出異常
  • should.doesNotThrow(block, [message]): 判斷是否不拋出異常
  • should.fail(actual, expected, message, operator): 判斷是否不等
  • should.ifError(err): 判斷是否為錯(cuò)誤
  • should.exist(actual, [message]): 判斷對(duì)象是否存在
  • should.not.exist(actual, [message]): 判斷對(duì)象是否不存在

另外should還提供了一系列類型判斷斷言方法:

// bool類型判斷
(true).should.be.true();
false.should.not.be.true();

// 數(shù)組是否包含
[ 1, 2, 3].should.containDeep([2, 1]);
[ 1, 2, [ 1, 2, 3 ]].should.containDeep([ 1, [ 3, 1 ]]);

// 數(shù)字比較
(10).should.not.be.NaN();
NaN.should.be.NaN();
(0).should.be.belowOrEqual(10);
(0).should.be.belowOrEqual(0);
(10).should.be.aboveOrEqual(0);
(10).should.be.aboveOrEqual(10);

// Promise狀態(tài)判斷
// don't forget to handle async nature
(new Promise(function(resolve, reject) { resolve(10); })).should.be.fulfilled();

// test example with mocha it is possible to return promise
it('is async', () => {
 return new Promise(resolve => resolve(10))
  .should.be.fulfilled();
});

// 對(duì)象的屬性判斷
({ a: 10 }).should.have.property('a');
({ a: 10, b: 20 }).should.have.properties({ b: 20 });
[1, 2].should.have.length(2);
({}).should.be.empty();

// 類型檢查
[1, 2, 3].should.is.Array();
({}).should.is.Object();

幾種常見的測試風(fēng)格代碼舉例

BDD

BDD提供的接口有:describe(), context(), it(), specify(), before(), after(), beforeEach(), and afterEach().

describe('Array', function() {
 before(function() {
  // ...
 });

 describe('#indexOf()', function() {
  context('when not present', function() {
   it('should not throw an error', function() {
    (function() {
     [1, 2, 3].indexOf(4);
    }.should.not.throw());
   });
   it('should return -1', function() {
    [1, 2, 3].indexOf(4).should.equal(-1);
   });
  });
  context('when present', function() {
   it('should return the index where the element first appears in the array', function() {
    [1, 2, 3].indexOf(3).should.equal(2);
   });
  });
 });
});

TDD

提供的接口有: suite(), test(), suiteSetup(), suiteTeardown(), setup(), and teardown():

suite('Array', function() {
 setup(function() {
  // ...
 });

 suite('#indexOf()', function() {
  test('should return -1 when not present', function() {
   assert.equal(-1, [1, 2, 3].indexOf(4));
  });
 });
});

QUNIT

和TDD類似,使用suite()和test()標(biāo)記測試永烈,包含的接口有:before(), after(), beforeEach(), and afterEach()。

function ok(expr, msg) {
 if (!expr) throw new Error(msg);
}

suite('Array');

test('#length', function() {
 var arr = [1, 2, 3];
 ok(arr.length == 3);
});

test('#indexOf()', function() {
 var arr = [1, 2, 3];
 ok(arr.indexOf(1) == 0);
 ok(arr.indexOf(2) == 1);
 ok(arr.indexOf(3) == 2);
});

suite('String');

test('#length', function() {
 ok('foo'.length == 3);
});

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Windows上安裝Node-RED的實(shí)現(xiàn)

    Windows上安裝Node-RED的實(shí)現(xiàn)

    Node-RED是一個(gè)用于物聯(lián)網(wǎng)編程的工具,提供了一個(gè)基于瀏覽器的編程環(huán)境和豐富的節(jié)點(diǎn)類型,本文就來介紹一下Windows上安裝Node-RED的實(shí)現(xiàn),感興趣的可以了解一下
    2025-02-02
  • 使用nvm和nrm優(yōu)化node.js工作流的方法

    使用nvm和nrm優(yōu)化node.js工作流的方法

    這篇文章主要介紹了使用nvm和nrm優(yōu)化node.js工作流的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • PostgreSQL Node.js實(shí)現(xiàn)函數(shù)計(jì)算方法示例

    PostgreSQL Node.js實(shí)現(xiàn)函數(shù)計(jì)算方法示例

    這篇文章主要給大家介紹了關(guān)于PostgreSQL Node.js實(shí)現(xiàn)函數(shù)計(jì)算的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • node.js中的fs.futimes方法使用說明

    node.js中的fs.futimes方法使用說明

    這篇文章主要介紹了node.js中的fs.futimes方法使用說明,本文介紹了fs.futimes方法說明、語法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • 談?wù)刵ode.js中的模塊系統(tǒng)

    談?wù)刵ode.js中的模塊系統(tǒng)

    這篇文章主要介紹了node.js中的模塊系統(tǒng),幫助大家更好的理解和學(xué)習(xí)node.js框架,感興趣的朋友可以了解下
    2020-09-09
  • koa2實(shí)現(xiàn)登錄注冊(cè)功能的示例代碼

    koa2實(shí)現(xiàn)登錄注冊(cè)功能的示例代碼

    這篇文章主要介紹了koa2實(shí)現(xiàn)登錄注冊(cè)功能的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • 詳解如何使用PM2將Node.js的集群變得更加容易

    詳解如何使用PM2將Node.js的集群變得更加容易

    本篇文章主要介紹了詳解如何使用PM2將Node.js的集群變得更加容易,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • Node.js通過身份證號(hào)驗(yàn)證年齡、出生日期與性別方法示例

    Node.js通過身份證號(hào)驗(yàn)證年齡、出生日期與性別方法示例

    最近工作中需要對(duì)身份證號(hào)的年齡、出生日期與性別進(jìn)行驗(yàn)證,所以這篇文章主要介紹了Node.js通過身份證號(hào)驗(yàn)證年齡、出生日期與性別的方法,在介紹完node.js的實(shí)現(xiàn)方法后又給大家分類的利用JS實(shí)現(xiàn)的方法,需要的朋友可以參考下。
    2017-03-03
  • Node.js中SerialPort(串口)模塊使用

    Node.js中SerialPort(串口)模塊使用

    本文主要介紹了Node.js中SerialPort(串口)模塊使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Node.js調(diào)用fs.renameSync報(bào)錯(cuò)(Error: EXDEV, cross-device link not permitted)

    Node.js調(diào)用fs.renameSync報(bào)錯(cuò)(Error: EXDEV, cross-device link not

    這篇文章主要介紹了Node.js調(diào)用fs.renameSync報(bào)錯(cuò)(Error: EXDEV, cross-device link not permitted),非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-12-12

最新評(píng)論

格尔木市| 清徐县| 临汾市| 武夷山市| 汉源县| 辽阳县| 平江县| 翁源县| 桐乡市| 南乐县| 太仓市| 河西区| 岳池县| 永仁县| 荣昌县| 安乡县| 兴城市| 巩留县| 通道| 泸西县| 施甸县| 绥中县| 泾源县| 新蔡县| 凤山市| 舟山市| 天峨县| 密云县| 涡阳县| 福贡县| 栾城县| 安溪县| 灵石县| 阳谷县| 江达县| 桑日县| 额济纳旗| 青海省| 嘉定区| 南岸区| 新乐市|