开玩笑实施

时间:2017-05-05 23:43:05

标签: react-native jestjs

我有一个带有此功能的文件

export const doSomething = (arg1, arg2) => {
  SomeClass.someFunction()
  OtherClass.otherFunction()
}

我看过人们如何在线模拟东西,但没有一个能解决我的问题。基本上在我的开玩笑测试中我想打电话

test('sendText to send text with the proper number', () => {
  doSomething()
  expect(SomeClass.someFunction).toBeCalled();
})

我不知道该怎么做。我在网上看到的所有内容都在测试函数的顶层模拟了一个函数,然后传递给他们想要测试的实际函数,这与我想要做的完全不同。

SomeClass是我为跟踪导入的第三方内容,我只是想验证它是否被调用。对于OtherClass也是如此。我该怎么做?

1 个答案:

答案 0 :(得分:0)

你可以通过模仿SomeClass来做到这一点。

import doSomething from './../doSomething';
import SomeClass from 'module/with/SomeClass';

jest.mock('module/with/SomeClass');

test('sendText to send text with the proper number', () => {
  doSomething()
  expect(SomeClass.someFunction).toBeCalled();
})

Jest应该能够确定第三方模块的结构并自动提供模拟。