前端測試入門
如何使用 Mocha 和 Chai 開始測試前端應用程式 警告:本篇文章已經過時,可能無法反映目前的最新技術狀態 Mocha 是一個強大且多功能的測試工具。市面上有很多測試框架,而我選擇 Mocha 是因為它的流行程度和易用性。 讓我們首先看看如何在瀏覽器中運行測試。下載以下文件: https://github.com/visionmedia/mocha https://github.com/chaijs/chai https://github.com/chaijs/chai-jquery 將相應的 mocha.js、chai.js、chai-jquery.js 文件放在您網站的 test/ 子文件夾中。 選擇您的 index.html 文件並載入它們,然後我們將設置 Mocha 使用 BDD 測試風格,並載入一個名為 test.web.js 的文件,該文件將承載我們的測試規則。 <script src="test/vendor/mocha.js" data-build-exclude="true"></script> <script src="test/vendor/chai.js" data-build-exclude="true"></script> <script src="test/vendor/chai-jquery.js" data-build-exclude="true"></script> <script data-build-exclude="true"> mocha.setup('bdd'); expect = chai.expect; </script> <script src="test/test.web.js" data-build-exclude="true"></script> 在您的 body 中,放置一個 div#mocha: <div id="mocha"></div> test/test.web.js 文件是整個遊戲的核心,它將承載測試規則。 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]....