nathanmacinnes/injectr

语言: JavaScript

git: https://github.com/nathanmacinnes/injectr

最后,一个node.js依赖注入的解决方案
Finally, a solution to node.js dependency injection
README.md (中文)

injectr

最后,一个node.js依赖注入的解决方案

安装它

npm install injectr。繁荣。

用它

var injectr = require('injectr');
var myScript = injectr('../lib/myScript.js', {
    fs : mockFs,
    crypto : mockCrypto
});

现在当你在myScript.js中需要('fs')或require('crypto')时,你是什么 get是mockFs或mockCrypto。

对你的测试要求injectr like require,并传递第二个参数 在你的模拟中。

路径现在相对于当前文件,就像需要一样。请更新 如果您从v0.4或更低版本升级,则进行测试。

上下文

injectr允许您通过访问injectr'd文件的上下文 可选的第三个参数。提供一个对象,injectr将修改它 必要时将其用作上下文。

var myScript = injectr('../lib/myScript.js', {}, {
    Date : mockDate,
    setTimeout : mockSetTimeout
});

从版本0.4开始,injectr不会为您创建完整的node.js上下文 使用。相反,它将您的脚本隔离在自己的沙箱中,允许您 包括只有你的脚本需要的位的模拟。

CoffeeScript的

injectr为您编译任何* .coffee文件,因此您可以测试您的 CoffeeScript也是。可以通过覆盖来更改默认设置 injectr.onload函数。它将文件名和文件内容作为 参数,并返回编译的脚本。

分享它

injectr属于麻省理工学院许可证。 叉吧。修改它。传递它。

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

injectr

Finally, a solution to node.js dependency injection

Install it

npm install injectr. Boom.

Use it

var injectr = require('injectr');
var myScript = injectr('../lib/myScript.js', {
    fs : mockFs,
    crypto : mockCrypto
});

Now when you require('fs') or require('crypto') in myScript.js, what you
get is mockFs or mockCrypto.

Treat injectr like require for your tests, with a second argument to pass
in your mocks.

Paths are now relative to the current file, just like require. Please update
your tests if you are upgrading from v0.4 or below.

Context

injectr gives you access to the context of the injectr'd file via
an optional third argument. Provide an object, and injectr will modify it
as necessary and use that as the context.

var myScript = injectr('../lib/myScript.js', {}, {
    Date : mockDate,
    setTimeout : mockSetTimeout
});

As of version 0.4, injectr doesn't create a full node.js context for you to
use. Instead, it isolates your script in its own sandbox, allowing you to
include mocks of only the bits that your script needs.

CoffeeScript

injectr compiles any *.coffee files for you, so you can test your
CoffeeScript too. The default settings can be changed by overwriting the
injectr.onload function. It takes the filename and file contents as
arguments, and returns the compiled script.

Share it

injectr is under the MIT License.
Fork it. Modify it. Pass it around.