不需要的节点ShouldJS

时间:2016-05-16 20:57:00

标签: node.js should.js

我的节点应用程序有一个相当大的测试套件,它依赖于ShouldJS模块。问题是库阻塞了在对象上分配.should属性,但我的测试套件需要启动需要可模拟的服务器(即在与我的测试套件相同的进程中运行)并动态构建ElasticSearch查询,需要在Objects上设置.should属性。

我试图使用方法described here取消对ShouldJS的要求。但这没有帮助 - 请参阅下面的示例脚本。有解决方法还是替代方法?

示例:

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 个答案:

答案 0 :(得分:1)

有一种方法可以让ShouldJS开启和关闭:

let placesClient = GMSPlacesClient()
    let filter = GMSAutocompleteFilter()
    filter.type = .Establishment
    filter.country = "SG"
    placesClient.autocompleteQuery(searchString, bounds: nil, filter: filter) { (results, error:NSError?) -> Void in
      self.resultsArray.removeAll()
      if let _ = results {
        for result in results!{
          if let res: GMSAutocompletePrediction = result {
           // your code
          }
        }
      }
    }

以下是官方自述文件所说的内容:

  

也可以使用不带getter的should.js(它甚至不会尝试扩展Object.prototype),只需要('should / as-function')。或者,如果您已经使用自动添加getter的版本,则可以调用.noConflict函数。

     

(某事)的结果。应该在大多数情况下得到和应该(某事)是相同的