Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jest mock:如何使用react native randombytes模拟随机输出_Javascript_React Native_Jestjs - Fatal编程技术网

Javascript jest mock:如何使用react native randombytes模拟随机输出

Javascript jest mock:如何使用react native randombytes模拟随机输出,javascript,react-native,jestjs,Javascript,React Native,Jestjs,我试图通过替换react native randombytes编写一个jest单元测试,因为它使用了一些NativeModules 我的错误消息: Test suite failed to run TypeError: Cannot read property 'seed' of undefined > 1 | const RandomBytes = jest.genMockFromModule('react-native-randombytes'); |

我试图通过替换
react native randombytes
编写一个jest单元测试,因为它使用了一些
NativeModules

我的错误消息:

Test suite failed to run

TypeError: Cannot read property 'seed' of undefined

> 1 | const RandomBytes = jest.genMockFromModule('react-native-randombytes');
    |                          ^
  2 | 
  3 | const randomBytes = (l) => {
  4 |   let uint8 = new Uint8Array(l);

  at seed (node_modules/react-native-randombytes/index.js:15:21)
  at Object.init (node_modules/react-native-randombytes/index.js:57:1)
  at Object.genMockFromModule (__mocks__/react-native-randombytes.js:1:26)
我将一个文件
react native randombytes
放在
节点模块旁边的
文件夹中

const RandomBytes = jest.genMockFromModule('react-native-randombytes');

const randomBytes = (l) => {
  let uint8 = new Uint8Array(l);
  uint8 = uint8.map(() => Math.floor(Math.random() * 90)+10);
  return uint8;
};

const seed = randomBytes(4096);

RandomBytes.randomBytes = randomBytes;
RandomBytes.seed = seed;

export default RandomBytes;
我打开了我想要模拟的库,发现它不是一个类,而是在index.js文件的末尾执行了下面的代码部分

似乎使用jest.genMockFromModule将触发init函数,因此整个模拟失败。我应该考虑哪些因素来选择使用哪种模拟方法?在文档中,它列出了各种方法,但何时使用这些方法并不明确

我应该使用jest.fn()吗

请给我一些建议

更新1:

我在测试文件中尝试了以下内容

jest.mock('react-native-randombytes');
const randomBytes = jest.fn().mockImplementation((l) => {
    let uint8 = new Uint8Array(l);
    uint8 = uint8.map(() => Math.floor(Math.random() * 90)+10);
    return uint8;
  });
结果:它不起作用。它也有同样的错误

更新2:在模拟中更改我的文件,如下所示

const R = jest.genMockFromModule('react-native-randombytes');

R.randomBytes = (l) => {
  let uint8 = new Uint8Array(l);
  uint8 = uint8.map(() => Math.floor(Math.random() * 90)+10);
  return uint8;
};

R.init = () => {};

export default R;
结果:相同的错误消息。它仍然是原始字节

更新3:就像更新1一样,但有些扭曲 受此启发

结果:相同的错误消息。

如果你是一个开玩笑的新手,请参考我的第一条

有许多节点_模块依赖于react native randombytes。追着他们跑你会发疯的。相反,您应该找到一个上层模块并对其进行模拟,如果它只是一个函数,则只需进行函数模拟。示例如下所示

我建议使用手动模拟,因为这些模块位于
node\u模块

const RandomBytes = jest.genMockFromModule('react-native-randombytes');

const randomBytes = (l) => {
  let uint8 = new Uint8Array(l);
  uint8 = uint8.map(() => Math.floor(Math.random() * 90)+10);
  return uint8;
};

const seed = randomBytes(4096);

RandomBytes.randomBytes = randomBytes;
RandomBytes.seed = seed;

export default RandomBytes;
例1:

jest.mock('react-native-securerandom', (size) => {
  return {
    generateSecureRandom: jest.fn(() => {
      let uint8 = new Uint8Array(size);
      uint8 = uint8.map(() => Math.floor(Math.random() * 90)+10);
      return uint8;
    }),
  };
});
例2:

'use strict';

const bip39 = jest.mock('react-native-bip39'); // genMockFromModule causes problem

bip39.generateMnemonic = jest.fn((l) => [
  'furth',
  'edessa',
  'injustices',
  'frankston',
  'serjeant',
  'khazar',
  'sihanouk',
  'longchamp',
  'stags',
  'pogroms',
  'coups',
  'upperparts',
  'endpoints',
  'infringed',
  'nuanced',
  'summing',
  'humorist',
  'pacification',
  'ciaran',
  'jamaat',
  'anteriorly',
  'roddick',
  'springboks',
  'faceted'
  ].slice(0, l));

bip39.validateMnemonic = jest.fn((_) => true);

bip39.mnemonicToSeed = jest.fn((_) => 'I am a mnemonic seed');

export default bip39;
请注意,
Math.random()
不是随机性的安全来源(请参阅)
'use strict';

const bip39 = jest.mock('react-native-bip39'); // genMockFromModule causes problem

bip39.generateMnemonic = jest.fn((l) => [
  'furth',
  'edessa',
  'injustices',
  'frankston',
  'serjeant',
  'khazar',
  'sihanouk',
  'longchamp',
  'stags',
  'pogroms',
  'coups',
  'upperparts',
  'endpoints',
  'infringed',
  'nuanced',
  'summing',
  'humorist',
  'pacification',
  'ciaran',
  'jamaat',
  'anteriorly',
  'roddick',
  'springboks',
  'faceted'
  ].slice(0, l));

bip39.validateMnemonic = jest.fn((_) => true);

bip39.mnemonicToSeed = jest.fn((_) => 'I am a mnemonic seed');

export default bip39;