【问题标题】:Testing: Mocha and node differ re fs.exists测试:Mocha 和 node 不同 refs.exists
【发布时间】:2016-07-21 13:26:18
【问题描述】:

首先,虽然我是一名经验丰富的程序员,但我对 node 比较陌生,对 mocha 也很陌生,所以我的问题可能是我!在这种情况下,我也很乐意发现这一点!

我正在编写的一些代码使用了一个 npm 模块,其中包含对 fs.exists 的调用。当我通过节点运行时使用 fs.exists 运行测试时,我会得到不同的结果。在我看来,我应该得到同样的答案;该文件确实存在,因此在这两种情况下结果都应该为真,但相反,通过节点调用时为真,通过 mocha 调用时为假。好吧,它运行不正常。

我知道 fs.exists 已被弃用,但那是在我正在使用的 npm 模块中,所以除非我修改模块,否则这就是我正在使用的。

差异与我调用 mocha 的方式有关。

根据一些网站建议,我编辑了我的 package.json 文件,在“脚本”部分中包含以下语句:“test”:“mocha test”,并且我已将我的测试放在这个“test”目录中.这是通过 npm test 调用的。

遇到问题后,我也安装了did npm install -g mocha。

我的测试文件是 testVersion.js

结果是通过node、npm test、mocha testVersion.js调用fs.exists时,调用以下三种方式之一得到不同的答案;我期待“真实”: - 节点 testVersion.js 返回 true - 它找到文件。 - mocha testVersion.js 返回 true - 它找到文件。 - npm test 返回 false - 它没有找到文件。

我怀疑可能在我身上调用了不同版本的 mocha 或 node,但我没有足够的经验来自行确定。

所以我的问题是:为什么我会得到不同的结果?

这是我的 testVersion.js 代码

var expect = require('chai').expect;
var assert = expect.assert;
var fs = require("fs");
var isInTest = typeof global.it === 'function';  //exe via mocha or node

console.log('isInTest mode: ', isInTest);

if(!isInTest) {
  console.log('NOT TEST MODE: invoking fs.exists');
  fs.exists("testVersion.js", function(info) {
    console.log('NOT TEST MODE: fs.exists callback RAN.');
    console.log('NOT TEST MODE: fs.exists: should be true and is: ', info);
  })
}



if(isInTest) {
  describe("Test Mocha and fs.exists issue", function() {
    it("Test that file self exists.", function() {
      console.log('TEST MODE: invoking fs.exists');
      expect(fs.exists("testVersion.js", function(result) {
        console.log('TEST MODE: fs.exists callback RAN.');
        console.log('TEST MODE: fs.exists: should be true, and is: ', result);
        return true;
      })).to.equal(true);
    });
  });
};

【问题讨论】:

  • 我猜测全局安装的 mocha 版本(即你运行 npm install -g mocha 时得到的版本)与 package.json 中指定的版本不同,这可能是什么npm 测试使用?
  • 你能添加你的 package.json(至少 devDependencies 位)吗?
  • ' "devDependencies": { "chai": "^3.5.0", "mocha": "^2.4.5", "mocha-sinon": "^1.1.5", " sinon": "^1.17.3" }, "scripts": { "test": "mocha test" },'
  • 想说“谢谢”马克!
  • 我也去了全局mocha (/usr/local/lib/node_modules/mocha) package.json,上面写着“version”:“2.4.5”

标签: node.js mocha.js


【解决方案1】:

经过大量测试工作,我相信我的问题是因为与 fs.exists 相关的异步调用。无论如何,以下内容对我有用,我想记录它以防它帮助其他人。

var assert = require('assert');
var fs = require('fs');

fs.existsSync("bbq.js", function(result) {
  console.log('False: fs.exists(bbq.js) says: ', result);
})

fs.existsSync("test2.js", function(result) {
  console.log('True: fs.exists(test2.js) says: ', result);
})

describe('Testing Synch fs.existsSync() ===', function() {
  describe('False: fs.exists(bbq.js)', function() {
    it("This assertion should pass, as we are asserting false on file that doesn't exist.", function() {
      assert.equal(false, fs.existsSync(__dirname + "/bbq.js", function(result) { return result;}))
    });
  });

  describe('Testing Synch fs.existsSync(test2.js method A)', function() {
    it("This assertions should pass as we are asserting true on file that does exist", function() {
      assert.equal(true, fs.existsSync(__dirname + "/test2.js", function(result) {
        return result;
      }));
    });
  });

  describe('Testing Synch fs.existsSync(test2.js method B)', function() {
    it("This assertions should pass as we are are asserting true on file that does exist using callback", function() {
      fs.existsSync(__dirname + "/test2.js", function(result) {
        assert.equal(true, result);
      });
    });
  });
});


////////////////////

describe('Asynch test of fs.exists() === some results are UNDEFINED because of async', function() {
  describe('False: fs.exists(bbq.js)', function() {
    it("This assertion should pass as we are expecting undefined result due to async.", function() {
      assert.equal(undefined, fs.exists(__dirname + "/bbq.js", function(result) { return result;}))
    });
  });

  describe('True: fs.exists(test2.js method A)', function() {
    it("This assertion should pass as we are expecting undefined result due to async.", function() {
      assert.equal(undefined, fs.exists(__dirname + "/test2.js", function(result) {
        return result;
      }));
    });
  });


  describe('True: fs.exists(test2.js method B)', function() {
    it("This equal assertion passes, because of use of callback waits for response.", function() {
      fs.exists(__dirname + "/test2.js", function(result) {
        assert.equal(true, result);
      });
    });
  });
});

【讨论】:

  • 几天前我再次阅读了您的问题,并意识到您在异步处理方面遇到了问题(当我在酒吧时!),但忘了回来添加答案, 对于那个很抱歉!很高兴你把它整理好了。如果您想测试异步流程,可以这样做 - 如果您需要帮助,请大声喊叫,我会添加一个答案,展示如何最好地完成!
猜你喜欢
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
相关资源
最近更新 更多