2015-02-09 196 views
-2

我使用Chrome擴展的生成器創建了一個項目。以下是我的文件夾結構的表示。我想測試common.js。我的摩卡的理解是,我應該有一個index.html文件是這樣的:摩卡測試,Yeoman,Chrome擴展

<!doctype html> 
<html> 
    <head> 
     <meta charset="utf-8"> 
     <title>Mocha Spec Runner</title> 
     <link rel="stylesheet" href="bower_components/mocha/mocha.css"> 
    </head> 
    <body> 
     <div id="mocha"></div> 
     <script src="bower_components/mocha/mocha.js"></script> 
     <script>  
      mocha.setup('bdd'); 
      mocha.reporter('html'); 
     </script> 
     <script src="bower_components/chai/chai.js"></script> 
     <script> 
      var assert = chai.assert; 
      var expect = chai.expect; 
      var should = chai.should(); 
     </script> 

     <!-- include source files here... --> 
     <script src="../app/scripts/common.js"></script> 

     <!-- include spec files here... --> 
     <script src="spec/test.js"></script> 

     <script>mocha.run()</script> 
    </body> 
</html> 

的測試文件(test.js)看起來是這樣的:

/* global describe, it */ 
describe('Find the root recipe node', function() { 
    it('Should not find an elem in null dom', function() { 
     console.log(dds_hmr.findRecipeRoot); 
    }); 
}); 

common.js文件看起來是這樣的:

var dds_hmr = {}; 
dds_hmr.findRecipeRoot = function (elem) { 
    if (elem && elem.hasAttribute("itemtype") 
      && elem.getAttribute("itemtype") === "http://schema.org/Recipe") { 
     return [elem]; 
    } else { 
     var result = []; 
     if (elem) { 
      for (var i = 0; i < elem.childNodes.length; i++) { 
       var child = dds_hmr.findRecipeRoot(elem.childNodes[i]); 
       result.concat(child); 
      } 
     } 
     return result; 
    } 
}; 

這是我得到的錯誤:

1) Find the root recipe node Should not find an elem in null dom: 
    ReferenceError: Can't find variable: dds_hmr 
     at http://localhost:9000/spec/test.js:4 
     at http://localhost:9000/bower_components/mocha/mocha.js:4263 
     at http://localhost:9000/bower_components/mocha/mocha.js:4635 
     at http://localhost:9000/bower_components/mocha/mocha.js:4694 
     at next (http://localhost:9000/bower_components/mocha/mocha.js:4561) 
     at http://localhost:9000/bower_components/mocha/mocha.js:4570 
     at next (http://localhost:9000/bower_components/mocha/mocha.js:4514) 
     at http://localhost:9000/bower_components/mocha/mocha.js:4538 
     at timeslice (http://localhost:9000/bower_components/mocha/mocha.js:5531) 

我需要做什麼才能使測試引用common.js文件中的代碼?

+0

對於那些反對投票,請解釋您的問題與問題或答案。據我所知,這是兩種模式。 – Virmundi 2015-02-10 12:45:03

+0

我認爲在您使用正確的信息編輯問題之前積累了積分。和你的答案一樣。 – Xan 2015-02-10 13:04:09

回答

0

原來,解決方案在於Grunt配置(有意義)。在grunt服務器的connect密鑰中有一個test密鑰。根據options屬性,還有另一個屬性base。將'app'添加到該列表。這將包括測試執行時的應用程序目錄。從那裏,唯一的辦法就是從

<!-- include source files here... --> 
<script src="../app/scripts/common.js"></script> 

<!-- include source files here... --> 
<script src="scripts/common.js"></script> 

更新改劇本在index.html包括:增加了繁重的部分

// Grunt server and debug server setting 
connect: { 
    options: { 
    port: 9000, 
    livereload: 35729, 
    // change this to '0.0.0.0' to access the server from outside 
    hostname: 'localhost' 
    }, 
    chrome: { 
    options: { 
     open: false, 
     base: [ 
     '<%= config.app %>' 
     ] 
    } 
    }, 
    test: { 
    options: { 
     open: false, 
     base: [ 
     'test', 
     'app', 
     '<%= config.app %>' 
     ] 
    } 
    } 
}