2017-01-16 246 views
0

我正在爲我的電子js應用程序編寫單元測試,而且我被困在一個地方。如何在電子模擬需求js

例如

比方說我的應用程序代碼是像下面

var app= (function() { 
    function app() { 
     var _this = this; 
     this.open = function (data) { 
      var deferred = Q.defer(); 
      try { 
       // some code 
      } 
      catch (ex) { 
       deferred.reject(ex); 
      } 
      return deferred.promise; 
     }; 

    } 
}()); 
exports.app = app 

現在,如果我想電子客戶端上運行它,它將會運行完美的罰款作爲電子模塊安裝客戶端的PC上

問題是,當我試圖編寫上述單元測試用例,因爲在下面的開發機器上沒有安裝電子設備

import { app} from '../../app' 
    import { Rights } from '../../Rights' 
    describe('app',() => { 
     let app: app; 
     beforeEach(() => { 
      app= new app(); 
     }) 

     it('should call open() and return error for null content', function (done) { 
      let output = app.open(null); 
      output 
      .then((res) => { 
       //expectation 
       done(); 
      }) 
      .catch((err)=>{ 
       //expectation 
       done(); 
      }) 
     }) 
    }) 

獲得以下錯誤

Error: Cannot find module 'electron'                                      
    at Function.Module._resolveFilename (module.js:469:15)                                 
    at Function.Module._load (module.js:417:25)                                   
    at Module.require (module.js:497:17)                                     
    at require (internal/module.js:20:19)                                     
    at Object.<anonymous> (<project pat>/app.js:2:16)                  
    at Module._compile (module.js:570:32)                                     
    at Object.Module._extensions..js (module.js:579:10)                                 
    at Module.load (module.js:487:32)                                      
    at tryModuleLoad (module.js:446:12)                                     
    at Function.Module._load (module.js:438:3)                                    
npm ERR! Test failed. See above for more details. 

問題

  • 如何嘲笑要求對未安裝dev的PC 上,但實際執行所需的任何模塊(安裝在客戶的電腦上) 。

回答

0

您可以通過覆蓋module._load攔截require電話:

const m = require('module'); 
const originalLoader = m._load; 
const stubs = { electron : {} }; 

m._load = function hookedLoader(request, parent, isMain) { 
    const stub = stubs[request]; 
    return stub || originalLoader(request, parent, isMain); 
}; 
+0

用一些小的變化你的解決方案.. :) –

0

我有過類似的問題與摩卡。我的基本方法是: *需要電子在前面的勾, *與本地默認 *需要應用 *鉤

這裏清理後的是一個樣本覆蓋它:

var el = require('../../modules/electron'); 
 

 
describe('app', function() { 
 
    'use strict'; 
 

 
    var el_override = { 
 
     post: function() { 
 
     }, 
 
     get: function() { 
 
     } 
 
    }, app; 
 

 
    before(function() { 
 
    /* Since we already have electron required, we override it 
 
     from node cache by: 
 
     require.cache['/path/to/file/name'].exports = el_override 
 
    */ 
 

 
    // Require app 
 
    // This will import overridden electron 
 
    App = require('app-location'); 
 
    }); 
 

 
    beforeEach(function() { 
 
    // init app 
 
    app = new App(); 
 
    }); 
 

 
    after(function() { 
 
    // Clean Up both electron override and app by 
 
    // delete requre.cache['/path/to/file/names'] 
 
    }); 
 

 
    it('should test for batch', function() { 
 
    // Call you functions 
 
    }); 
 
});

+0

但是,你需要在開發機器上安裝電子以及.. .. ..? –

+0

是在開發機器上安裝正常的依賴關係以及dev。在生產上安裝正常的依賴關係。 –

+0

這就是我想要避免的... –