<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        PHPStorm中如何對nodejs項目進行單元測試詳解

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:00:33
        文檔

        PHPStorm中如何對nodejs項目進行單元測試詳解

        PHPStorm中如何對nodejs項目進行單元測試詳解:安裝必要的包 nodejs的單元測試最常用的是使用mocha包。首先確保你本地安裝nodejs,之后按照mocha包。 npm install mocha -g 然后還需要安裝相關(guān)的斷言工具,Node.js中常用的斷言庫有: assert: TDD風(fēng)格 should: BDD風(fēng)格 expect
        推薦度:
        導(dǎo)讀PHPStorm中如何對nodejs項目進行單元測試詳解:安裝必要的包 nodejs的單元測試最常用的是使用mocha包。首先確保你本地安裝nodejs,之后按照mocha包。 npm install mocha -g 然后還需要安裝相關(guān)的斷言工具,Node.js中常用的斷言庫有: assert: TDD風(fēng)格 should: BDD風(fēng)格 expect

        安裝必要的包

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

        npm install mocha -g

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

      1. assert: TDD風(fēng)格
      2. should: BDD風(fēng)格
      3. expect: BDD風(fēng)格
      4. chai: BDD/TDD風(fēng)格
      5. 使用npm install安裝這些斷言庫其中之一即可。

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

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


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

      6. Name: 隨便一個運行配置的名稱,如MochaTest
      7. Working directory: 當(dāng)前項目目錄
      8. Mocha package: Mocha安裝包的目錄,node_modules\mocha
      9. User interface: 測試類型,這里選擇TDD(對應(yīng)assert庫)
      10. Test directory: 這一項可以選擇測試目錄或文件
      11. All in directory: 整個目錄都進行測試
      12. File patterns: 某種模式的文件,可以填正則表達式
      13. Test file: 某個特定的測試文件
      14. 填寫完成并且沒有報錯后點擊OK。

        Nodejs進行單元測試

        這里我們選擇assert庫,TDD模式進行單元測試。在上面選定的Test directory目錄下新建一個測試文件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));
         });
         });
        });

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

        斷言庫的使用

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

        assert庫:TDD風(fēng)格

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

      15. assert.fail(actual, expected, message, operator)
      16. assert(value, message), assert.ok(value, [message])
      17. assert.equal(actual, expected, [message])
      18. assert.notEqual(actual, expected, [message])
      19. assert.deepEqual(actual, expected, [message])
      20. assert.notDeepEqual(actual, expected, [message])
      21. assert.strictEqual(actual, expected, [message])
      22. assert.notStrictEqual(actual, expected, [message])
      23. assert.throws(block, [error], [message])
      24. assert.doesNotThrow(block, [message])
      25. assert.ifError(value)
      26. 其中的參數(shù)說明如下:

      27. value: 實際值
      28. actual: 實際值
      29. expected: 期望值
      30. block: 語句塊
      31. message: 附加信息
      32. 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)用,功能非常強大。相關(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

      33. should.equal(actual, expected, [message]): 判斷是否相等
      34. should.notEqual(actual, expected, [message]): 判斷是否不相等
      35. should.strictEqual(actual, expected, [message]): 判斷是否嚴(yán)格相等
      36. should.notStrictEqual(actual, expected, [message]): 判斷是否嚴(yán)格不相等
      37. should.deepEqual(actual, expected, [message]): 判斷是否遞歸相等
      38. should.notDeepEqual(actual, expected, [message]): 判斷是否遞歸不想等
      39. should.throws(block, [error], [message]): 判斷是否拋出異常
      40. should.doesNotThrow(block, [message]): 判斷是否不拋出異常
      41. should.fail(actual, expected, message, operator): 判斷是否不等
      42. should.ifError(err): 判斷是否為錯誤
      43. should.exist(actual, [message]): 判斷對象是否存在
      44. should.not.exist(actual, [message]): 判斷對象是否不存在
      45. 另外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();
        });
        
        // 對象的屬性判斷
        ({ 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é)

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        PHPStorm中如何對nodejs項目進行單元測試詳解

        PHPStorm中如何對nodejs項目進行單元測試詳解:安裝必要的包 nodejs的單元測試最常用的是使用mocha包。首先確保你本地安裝nodejs,之后按照mocha包。 npm install mocha -g 然后還需要安裝相關(guān)的斷言工具,Node.js中常用的斷言庫有: assert: TDD風(fēng)格 should: BDD風(fēng)格 expect
        推薦度:
        標(biāo)簽: js 詳解 單元測試
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲VA中文字幕无码毛片| 久久精品国产亚洲一区二区三区| 久久国产成人亚洲精品影院| 狠狠综合亚洲综合亚洲色| 免费国产va在线观看| 好爽好紧好大的免费视频国产| 日韩成人免费aa在线看| 伊人久久亚洲综合影院首页| 免费观看理论片毛片| 国产精品亚洲精品日韩电影| 免费一级做a爰片性色毛片| 亚洲AV本道一区二区三区四区| 久久99免费视频| 大学生一级特黄的免费大片视频| 中文字幕亚洲综合小综合在线| 在线jyzzjyzz免费视频| 国产精品亚洲AV三区| 亚洲国产婷婷综合在线精品| 全黄大全大色全免费大片| 久久精品国产亚洲精品2020| 91网站免费观看| 久久久久亚洲精品无码网址| 男女一边桶一边摸一边脱视频免费| 99免费在线观看视频| 亚洲黄片毛片在线观看| 国产在线观看无码免费视频| 内射干少妇亚洲69XXX| 无人在线观看免费高清视频| 亚洲精品自产拍在线观看动漫| 2022久久国产精品免费热麻豆| 亚洲日韩精品无码一区二区三区| 久久免费国产视频| 亚洲裸男gv网站| 污污网站免费观看| 亚洲精品乱码久久久久久久久久久久 | 日本一区二区在线免费观看| 亚洲AV午夜成人影院老师机影院| 黄瓜视频影院在线观看免费| 三级片免费观看久久| 亚洲色欲或者高潮影院| 免费无码又爽又刺激高潮的视频 |