嘲笑在同一模块内导出和调用的模块function?

新的unit testing和间谍,存根和嘲笑的概念。

我想从下面的代码testingpassword.jsverify方法,但是我无法在testing文件中存储hash函数。

由于verify使用hash函数和hash函数导出,我虽然我应该stub hash函数返回一个固定的响应,而不是实际调用hash 。 因为我不想testinghash函数。

问题:testingverify时不会调用创build的hash函数存根。

问题1:我应该把重点放在testing函数本身的逻辑上而不是另一个函数上吗?

主要问题:(已 回答)如何在同一个模块中调用模块函数?

方面的问题2:如果我不知道哪里没有导出,而只停留在模块中,我将如何去做哈希哈希?

password.js

 /** * Creates a hash based on a salt from a given password * if there is no salt a new salt will be generated * * @param {String} password * @param {String} [salt] Optional value, if not given will generate a salt */ function hash (password, salt) { // returns a promise that resolves an hash object with hash and salt key // example: {hash: '2512521nska...', salt: '25hbBhhsfa...'} } /** * Verifies if a password matches a hash by hashing the password * with a given salt * * @param {String} password * @param {String} hashString * @param {String} salt */ function verify (password, hashString, salt) { return hash(password, salt) .then((res) => res.hash === hashString); } module.exports = {hash, verify}; 

password.test.js

 import test from 'ava'; import sinon from 'sinon'; import passwordModule from './password'; test('verify - should verify password', function * (t) { const password = 'test-password'; const salt = null; const hash = 'my-hash'; const hashStub = sinon.stub(passwordModule, 'hash', (password, salt) => Promise.resolve({hash, salt})); const verified = yield passwordModule.verify(password, hash, salt); t.true(verified); hashStub.restore(); }); 

build立

  • 节点v6.2.0
  • 艾娃v0.15.2
  • Sinon“v1.17.4

testing和模块与babel transpilled。 但是该模块没有使用ES6模块导出,因为它在节点env中使用,没有使用transpilling。
我正在testing所有的代码,以便将来可以certificate,并且save env可以用于前端代码和后端代码的前端代码。

问题1:我应该把重点放在testing函数本身的逻辑上而不是另一个函数上吗?

部分testingverify是确保它正确地调用hash 。 还有一个更一般的意义,并不太适用于你的代码,是一个函数应该处理由其他函数正确抛出的错误。 在你的情况下,你将任何错误传播给verify的调用者,这就是为什么它不适用。

主要问题:如何在同一个模块中调用模块函数?

你已经find了答案,但是看下面的替代scheme。

方面的问题2:如果我不知道哪里没有导出,而只停留在模块中,我将如何去做哈希哈希?

一个伟大的模块是rewire ,它允许你覆盖模块内部的私有 (非导出)variables。 它也将帮助你的“主要问题”,因为它允许你像以前一样离开代码。

这是你的rewiretesting:

 import test from 'ava'; import sinon from 'sinon'; import rewire from 'rewire'; const passwordModule = rewire('./password'); test('verify - should verify password', function * (t) { const password = 'test-password'; const salt = null; const hash = 'my-hash'; let hashStub = sinon.stub().returns(Promise.resolve({hash, salt})); // Replace the `hash` function inside your module with the stub. let revert = passwordModule.__set__('hash', hashStub); const verified = yield passwordModule.verify(password, hash, salt); t.true(verified); // Revert to the original `hash` function. revert(); }); 

在stackoverflow中find主要问题的答案:
从同一模块调用的存根模块function

要解决这个问题,我需要调用散列导出的hash函数,而不是私人。

 exports.hash = function hash (password, salt) { // returns a promise that resolves an hash object with hash and salt key // example: {hash: '2512521nska...', salt: '25hbBhhsfa...'} } exports.verify = function verify (password, hashString, salt) { return exports.hash(password, salt) .then((res) => res.hash === hashString); } 

还想知道旁边的问题的答案。