簡體   English   中英

如何在electronJS中模擬需求

[英]How to mock require in electronJS

我正在嘗試為我的electronic 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.   

  • 如何模擬需要開發PC上未安裝但實際執行需要的任何模塊(安裝在客戶端PC上)。

您可以通過覆蓋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);
};

摩卡咖啡也有類似的問題。 我的基本方法是:*鈎子之前需要輸入電子,*本地默認值將其覆蓋*要求應用程序*鈎子之后清除

這是一個示例:

 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 }); }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM