【问题标题】:Jest Mocking Out Implementation开玩笑模拟实施
【发布时间】:2017-05-05 23:43:05
【问题描述】:

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

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 也一样。我该怎么做?

【问题讨论】:

    标签: react-native jestjs


    【解决方案1】:

    您可以通过模拟 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 应该能够确定第三方模块的结构并自动提供模拟。

    【讨论】:

      猜你喜欢
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 2020-10-20
      • 2021-05-22
      • 2023-03-08
      相关资源
      最近更新 更多