Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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
Unit testing 开玩笑–;如何模拟来自模块的非默认导出?_Unit Testing_React Native_Mocking_Jestjs - Fatal编程技术网

Unit testing 开玩笑–;如何模拟来自模块的非默认导出?

Unit testing 开玩笑–;如何模拟来自模块的非默认导出?,unit-testing,react-native,mocking,jestjs,Unit Testing,React Native,Mocking,Jestjs,我试图模拟react native中的NativeModules,但是我找不到一种方法来只模拟该类,而不是整个react native模块 基本上,在我的生产代码中,我是这样做的: import { NativeModules } from 'react-native' const { MyCustomNativeModule } = NativeModules 在测试中,我想重写MyCustomNativeModule。目前,我找到的唯一方法是模拟整个react native模块,如下所示:

我试图模拟
react native
中的
NativeModules
,但是我找不到一种方法来只模拟该类,而不是整个
react native
模块

基本上,在我的生产代码中,我是这样做的:

import { NativeModules } from 'react-native'
const { MyCustomNativeModule } = NativeModules
在测试中,我想重写
MyCustomNativeModule
。目前,我找到的唯一方法是模拟整个
react native
模块,如下所示:

// /__mocks__/react-native.js

module.exports = {
  NativeModules: {
    MyCustomNativeModule: {
      dismiss: () => {},
    },
  },
}
但这破坏了所有其他的
react原生
函数。我看到人们经常使用类似于
jest.mock('NativeModules',()=>…)
的方法,但这似乎真的不起作用

这个呢:

课程

// sound-player.js
 export default class SoundPlayer {
   constructor() {
    this.foo = 'bar';
 }

  playSoundFile(fileName) {
   console.log('Playing sound file ' + fileName);
 }
}
import SoundPlayer from './sound-player';
const mockPlaySoundFile = jest.fn();
jest.mock('./sound-player', () => {
   return jest.fn().mockImplementation(() => {
       return {playSoundFile: mockPlaySoundFile};
    });
  });
开玩笑地嘲笑。mock()

import SoundPlayer from './sound-player';
const mockPlaySoundFile = jest.fn();
jest.mock('./sound-player', () => {
   return jest.fn().mockImplementation(() => {
       return {playSoundFile: mockPlaySoundFile};
    });
  });
更新

import SoundPlayer from './sound-player';
const mockPlaySoundFile = jest.fn();
jest.mock('./sound-player', () => {
   return jest.fn().mockImplementation(() => {
       return {playSoundFile: mockPlaySoundFile};
    });
  });
这样怎么样:

function mockFunctions() {
  const original = require.requireActual('../myModule');
  return {
    ...original, //Pass down all the exported objects
    test: jest.fn(() => {console.log('I didnt call the original')}),
    someFnIWantToCurry: {console.log('I will curry the original') return jest.fn((...args) => original.someFnIWantToCurry(...args)}),
  }
jest.mock('../myModule', () => mockFunctions());
const storage = require.requireMock('../myModule');

请参阅:

以下是使用
jest.mock
手动模拟
react native
模块的解决方案

为了保持简单,我模拟了
react native
模块。您可以使用real
react native
替换模拟的

文件结构为:

。
├── 索引规范ts
├── 索引
└── react-native.ts
模拟
react-native
模块:

react native.ts

const NativeModules={
MyCustomNativeModule:{
解雇:()=>{
//原始实现
返回“真实数据”;
}
}
};
导出{NativeModules};
index.ts
,假设您导入并使用此文件中的
react native
模块:

从“/react native”导入{NativeModules};
导出函数main(){
返回NativeModules.MyCustomNativeModule.disease();
}
单元测试,
索引规范ts

从“/”导入{main};
从“/react native”导入{NativeModules};
jest.mock('./react native',()=>{
返回{
本机模块:{
MyCustomNativeModule:{
解雇:jest.fn()
}
}
};
});
描述('main',()=>{
它('should mock react native correct',()=>{
常量mockedData='模拟数据';
(NativeModules.MyCustomNativeModule.Discouse as jest.MockedFunction<
NativeModules.MyCustomNativeModule.My的类型
>).mockedReturnValueOnce(mockedData);
const actualValue=main();
expect(实际值).toBe(模拟数据);
期望(NativeModules.MyCustomNativeModule.discouse).toBeCalledTimes(1);
});
});
100%覆盖率的单元测试结果:

PASS src/stackoverflow/54393006/index.spec.ts
主要的
✓ 模拟应正确反应(19ms)
----------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
----------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 100 | 100 ||
index.ts | 100 | 100 | 100 | 100 ||
----------|----------|----------|----------|----------|-------------------|
测试套件:1个通过,共1个
测试:1项通过,共1项
快照:共0个
时间:5.096秒

这是完整的演示:

此解决方案的问题在于它完全替换了
react native
模块,而不是该模块中的
{NativeModules}
更改了上述答案