我目前正在尝试使用https://github.com/felixge/node-sandboxed-module以便能够在单元测试中注入依赖模拟。结果这个模块杀死了在沙盒模块中创建的对象的 should.js:
我的模块.js:
module.exports = {
func1: function () {
return {
'THIS': {
'IS': {
'SPARTA': {
'DONT': 'TRUST ME'
}
}
}
};
}
};
myModule-test.js:
var should = require('should');
var sandboxedModule = require('sandboxed-module');
var myModule1 = require('./myModule');
var myModule2 = sandboxedModule.require('./myModule');
describe('myModule', function () {
it('should return the object', function () {
myModule1.func1().should.be.instanceOf(Object);
});
describe('returned object', function () {
it('should have the correct properties', function () {
myModule1.func1().THIS.should.have.property('IS');
});
});
});
describe('Sandboxed myModule', function () {
describe('returned object', function () {
it('should have the should property', function () {
should.exist(myModule2.func1().should);
});
describe('nested objects', function () {
it('should have the should property', function () {
should.exist(myModule2.func1().THIS.should);
should.exist(myModule2.func1().THIS.IS.should);
});
});
});
});
这些关于沙盒模块的测试失败:
1) Sandboxed myModule returned object should have the should property:
AssertionError: expected undefined to exist
2) Sandboxed myModule returned object nested objects should have the should property:
AssertionError: expected undefined to exist
我尝试为 Object 构造函数提供服务,以确保原型中应该隐藏的属性可用,但这也不起作用:
var myModule2 = sandboxedModule.require('./myModule', {
globals: {
Object: Object
}
});
有趣的是,如果我使用类似的沙箱模块,例如https://github.com/nathanmacinnes/injectr ,也会出现同样的问题。这让我很困惑谁在这里做错了:节点沙盒模块和注入器,节点本身,should.js,甚至是我?:)
谢谢你的帮助。