简体   繁体   English

我想测试是否在生命周期方法 componentDidMount 中调用了某个方法,但出现错误

[英]I want to test whether a method is called within the lifecycle method componentDidMount, but I am getting an error

I have a lifecycle method, componentDidMount that is calling a function, setDateOnMount which will be a spy within it.我有一个生命周期方法, componentDidMount ,它正在调用一个函数, setDateOnMount ,它将成为其中的一个间谍。 I am using jest and enzyme to test whether setDateOnMount runs or not, and I am getting an error that it is not called.我正在使用 jest 和酶来测试setDateOnMount是否运行,并且我收到一个错误,它没有被调用。

I have seen posts with the wrapper being called with mount rather than shallow , but I can't use mount because I am using the react-dates package, and it is conflicting with mount, where I cannot run it.我见过使用mount而不是shallow调用包装器的帖子,但我不能使用 mount 因为我使用的是 react-dates 包,并且它与 mount 冲突,我无法运行它。

Here is my test:这是我的测试:

test('componentDidMount should be called with setDateOnMount', () => {
        const setDateOnMount = jest.fn()
        const location = {
            state: undefined
        }
        const componentDidMount = jest.spyOn(MealSummary.prototype, 'componentDidMount')
        const wrapper = shallow(<MealSummary meals={meals} location={location} setDateOnMount={setDateOnMount} />)

        expect(componentDidMount).toHaveBeenCalledTimes(1)
        expect(setDateOnMount).toHaveBeenCalledTimes(1)  //errors here that it is not called
    })

The partial component I am testing is here:我正在测试的部分组件在这里:

.
.
 componentDidMount() {
        this.setDateOnMount()
    }
.
.
...

How can I get it to work?我怎样才能让它工作?

To test if setDateOnMount is called or not, you can mock or spy on it:要测试是否setDateOnMount ,您可以模拟或监视它:

  • Spy :间谍:
test('componentDidMount should be called with setDateOnMount', () => {
  const setDateOnMount = jest.spyOn(MealSummary.prototype, 'setDateOnMount') // spy
  const wrapper = shallow(<MealSummary meals={[]} location={{state: undefined}}  />) // create
  expect(setDateOnMount).toHaveBeenCalledTimes(1) // test
})
  • Mock :嘲笑 :
test('componentDidMount should be called with setDateOnMount', () => {
  const setDateOnMount = MealSummary.prototype.setDateOnMount = jest.fn() // mock
  const wrapper = shallow(<MealSummary meals={[]} location={{state: undefined}}  />) // create
  expect(setDateOnMount).toHaveBeenCalledTimes(1) // test
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sinon - 如何存根我想测试的方法调用的方法 - Sinon - How to stub a method called by the method I want to test 如何测试是否在componentDidMount中调用了方法? - How to test if method was called inside componentDidMount? 模拟componentDidMount生命周期方法进行测试 - Mock componentDidMount lifecycle method for testing React JS:为什么我在 componentdidMount() 中出错? - React JS: Why am I getting error at componentdidMount()? “componentDidMount”生命周期方法不更新状态 - The 'componentDidMount' lifecycle method does not update the state 为什么我的对象方法出现“不是函数错误”? - why am I getting “is not a function error” for my object method? 检查 `News` 的渲染方法。 我在 reactjs 中收到此错误 - Check the render method of `News`. i am getting this error in reactjs 是否可以确定是否从原型方法中调用了实例方法? - Is it possible to determine whether an instance method was called from within a prototype method? componentDidMount生命周期方法中的条件异步动作不断循环 - conditional async actions in componentDidMount lifecycle method keeps going on a loop 我想使用 fetch 方法从 api url 获取请求。 但我不断收到错误 400 - I want to get request from an api url using fetch method. But I keep on getting error 400
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM