Unit testing 我该如何开玩笑地模仿本地ibeacon

Unit testing 我该如何开玩笑地模仿本地ibeacon,unit-testing,react-native,jestjs,Unit Testing,React Native,Jestjs,我试图在本机模块上模拟react native ibeacon,我只想测试它的调用方式,包括下面Beacons对象中的所有函数 下面是一段未定义信标的代码片段: var React = require('react-native'); var Beacons = require('react-native-ibeacon'); jest.mock('react-native-ibeacon'); describe('beaconView', () => { console.log(

我试图在本机模块上模拟react native ibeacon,我只想测试它的调用方式,包括下面Beacons对象中的所有函数

下面是一段未定义信标的代码片段:

var React = require('react-native');
var Beacons = require('react-native-ibeacon');
jest.mock('react-native-ibeacon');

describe('beaconView', () => {

  console.log('Beacons', Beacons);

  Beacons.requestWhenInUseAuthorization();

  it('test pass', () => {
    expect(1).toBeTruthy();
  });
});
当我尝试调用RequestWhenUseAuthorization方法时,它失败了


我遗漏了什么?

您需要使用jest.mock的第二个参数提供一个好的mock

例如:

jest.mock('my-module', () => ({
    myFn: jest.fn();
}));
然后你可以做:

const myModule = require('my-module');

myModule.myFn() // calling the mock function.

您需要弄清楚外部本机模块具有哪些功能,然后创建一个行为类似的模拟。

我认为问题在于,信标依赖于javascript和iOS之间的桥梁,而当我使用jest运行时,这是不存在的。很好,我将朝着这个方向出发。非常感谢。