2016-05-16 90 views
0

我的節點應用程序有一個相當大的測試套件,取決於ShouldJS模塊。問題是庫阻塞了在對象上分配.should屬性,但是我的測試套件需要啓動一個需要可嘲弄的服務器(即運行在與我的測試套件相同的進程中),並動態構建需要設置的ElasticSearch查詢對象上的屬性.should不需要的節點ShouldJS

我試圖不要求ShouldJS使用方法described here。但是這並沒有幫助 - 請參閱下面的示例腳本。有解決方法或替代方法嗎?

實施例:

require('should'); 

var objBefore = {}; 
objBefore.should = 'should1'; // doesn't get assigned! 
objBefore['should'] = 'should2'; // doesn't get assigned! 
console.log('Before:', objBefore); 

Object.keys(require.cache).forEach(function(path) { 
    if (path.indexOf('/node_modules/should/') === -1) return; 
    console.log('Un-requiring path', path); 
    delete require.cache[path]; 
}); 

var objAfter = {}; 
objAfter.should = 'should1'; // doesn't get assigned! 
objAfter['should'] = 'should2'; // doesn't get assigned! 
console.log('After:', objAfter); 

// still has shouldJS stuff 
console.log('objAfter.should:', objAfter.should); 

其給出這樣的:

Before: {} 
Un-requiring path /my/path/node_modules/should/index.js 
Un-requiring path /my/path/node_modules/should/lib/should.js 
... 
Un-requiring path /my/path/node_modules/should/lib/ext/contain.js 
Un-requiring path /my/path/node_modules/should/lib/ext/promise.js 
After: {} 
objAfter.should: Assertion { obj: {}, anyOne: false, negate: false, params: { actual: {} } } 

.should屬性都未得到分配。

回答

1

有轉ShouldJS應的getter和關閉方式:

require('should'); 
(1).should.be.equal(1); 

// turn off ShouldJS should getter 
// and use should as a function 
var should = require('should').noConflict(); 
should(0).be.equal(0); 
// turn on ShouldJS should getter 
should.extend(); 

require('should'); 
(1).should.be.equal(1); 

下面是官方的自述說:

也可以使用should.js不消氣(它會甚至不嘗試擴展Object.prototype),只需要('should/as-function')。或者,如果您已經使用自動添加getter的版本,則可以調用.noConflict函數。

的(東西)的結果。應該getter和應(東西)在大多數情況下都是一樣的