【问题标题】:Proxyquire: Cannot Stub fs.readFileSyncProxyquire:无法存根 fs.readFileSync
【发布时间】:2017-12-23 22:13:50
【问题描述】:

我似乎无法从 NodeJS 核心对 fs 存根 readFileSync。以下代码隔离了该问题。通过 Mocha 运行测试结果如下:

> mocha tests/test.js

      Some description
        1) "before all" hook


      0 passing (15ms)
      1 failing

      1) Some description "before all" hook:
         TypeError: Cannot read property 'charCodeAt' of undefined
          at Object.stripBOM (internal/module.js:48:14)
          at Object.require.extensions.(anonymous function) (node_modules/proxyquire/lib/proxyquire.js:276:43)
          at Proxyquire._withoutCache (node_modules/proxyquire/lib/proxyquire.js:179:12)
          at Proxyquire.load (node_modules/proxyquire/lib/proxyquire.js:136:15)
          at Context.<anonymous> (tests/test.js:12:15)

这里是测试/test.js

var proxyquire = require('proxyquire'),
    sinon = require('sinon'),
    fs = require('fs'),
    sut;

describe('Some description', function () {
    var readFileSyncStub;

    before(function () {
        readFileSyncStub = sinon.stub(fs, 'readFileSync');
        readFileSyncStub.withArgs('someFile.js', { encoding: 'utf8' }).returns('some text');
        sut = proxyquire('../sut', { fs: { readFileSync: readFileSyncStub } }); // Exception encountered somewhere in here ...
    });

    after(function () {
        fs.readFileSync.restore(); // This is executed ...
    });

    it('Some it', function () {
        // This never happens ...
    });
});

这里是 sut.js,这是正在测试的模块:

var fs = require('fs'); // The code in this file is never executed ...

module.exports = function () {
    return fs.readFileSync('someFile.js', { encoding: 'utf8' });
};

项目的文件夹结构为:

./sut.js
./package.json
./tests/test.js

test.js 可以通过在提示符下执行mocha tests/test.js 来运行。

我注意到几年前在 Github 上报告了一个关于某些看起来相似的问题,但我不知道这是同一个问题还是不同的问题。这是链接:

https://github.com/thlorenz/proxyquire/issues/12

如果有帮助,这些是我的 package.json 文件中的依赖项。我尝试使用与 proxyquire 依赖项类似的版本:

{
  ...
  "devDependencies": {
    "jshint": "^2.9.5",
    "jslint": "^0.11.0",
    "mocha": "~3.1",
    "proxyquire": "^1.8.0",
    "sinon": "~1.9"
  },
  "dependencies": {}
  ...
}

非常感谢任何帮助!

【问题讨论】:

    标签: node.js unit-testing sinon proxyquire


    【解决方案1】:

    如果您使用proxyquire 替换它,则不需要存根fs.readFileSync()(实际上,存根fs.readFileSync() 会导致您的问题,因为它同时破坏了@ 987654325@ 和 proxyquire)。

    试试这个:

    describe('Some description', function () {
      var readFileSyncStub;
    
      before(function() {
        readFileSyncStub = sinon.stub()
                                .withArgs('someFile.js', { encoding: 'utf8' })
                                .returns('some text');
        sut = proxyquire('../sut', { fs : { readFileSync : readFileSyncStub } });                         
      });
    
      it('Some it', function () {
        let value = sut();
        assert.equal(value, 'some text');
      });
    
    });
    

    【讨论】:

    • 谢谢@robertklep,效果很好!不过,我有点困惑。 Github 上的示例为 fs 的方法存根:github.com/thlorenz/proxyquire/blob/master/examples/sinon/…
    • @Andrew 这些示例已经过时(该特定文件为 5 年)。我想与此同时,Node.js 的内部结构发生了变化。仅存根 fs.readFileSync() 会导致最近的 Node.js 版本出现问题。
    • 我如何能够断言在这种情况下调用了 readFileSyncStub ?我可以在 readFileSyncStub 上执行 sinon.assert.calledOnce 或 sinon.assert.calledExactly 吗?
    • @Novice 你试过了吗? ;D
    猜你喜欢
    • 2021-09-23
    • 2018-05-26
    • 2018-05-05
    • 2017-01-27
    • 2015-07-08
    • 2019-08-18
    • 2021-01-21
    • 2018-08-12
    相关资源
    最近更新 更多