模拟在同一模块中导出和调用的模块函数?

Mocking a module function which is exported and called within the same module?

单元测试和间谍、存根和模拟的概念的新手。

我想从下面的代码中测试 password.js 中的 verify 方法,但我无法 stub 测试文件中的 hash 函数。

由于verify使用了hash函数并且导出了hash函数,所以我应该stubhash函数[=69] =] 固定响应而不是实际调用 hash。因为我不想测试 hash 函数。

问题:测试verify.

时未调用为hash函数创建的存根

附带问题 1: 我应该专注于测试函数本身的逻辑而不是其他被调用函数的逻辑吗?

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

附带问题 2: 如果它没有导出但仅保留在模块中,我将如何进行存根 hash

代码

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();
});

设置

Tests and the modules are transpilled with babel. But the module isn't using ES6 module exports as it is used in node env without transpilling.
I'm transpilling all code during testing so that it is future proof and the save env can be used for both frontend and backend code, where the frontend code is transpilled.

在 Whosebug 中找到主要问题的答案:

为了解决这个问题,我需要调用散列导出的 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);
}

还是想知道边题的答案。

Side question 1: Should I be focusing on testing the logic of the function itself rather the other called functions?

测试 verify 的一部分是确保它正确调用 hash。同样在更一般的意义上,并不太适用于您的代码,一个函数应该正确处理其他函数抛出的错误。在您的情况下,您正在将任何错误传播给 verify 的调用者,这就是它实际上并不适用的原因。

Main Question: How do stub a module function that is called within the same module?

您已经找到了答案,但请参阅下面的备选方案。

Side question 2: How would I go about stubbing hash if it where not exported but stayed only within the module?

一个很棒的模块是 rewire,它允许您覆盖模块内的 private(非导出)变量。它还会对您的 "main question" 有所帮助,因为它允许您像以前一样保留代码。

这是你在 rewire 上的测试:

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();
});