programing

컴포넌트 메서드를 모사하는 방법

bestcode 2022. 7. 13. 23:18
반응형

컴포넌트 메서드를 모사하는 방법

단순히 스토어 액션 후에 컴포넌트 메서드가 호출되었는지 확인하려고 하는데 다음 오류가 나타납니다.

expect(jest.fn())[.not].toHaveBeenCalled()

jest.fn() value must be a mock function or spy.
Received:
  function: [Function bound mockConstructor]

다음은 유닛 테스트입니다.

describe('MyComponent.spec.js', () => {
  let methods = {
    setLocation: jest.fn()
    // more methods...
  }

  it('calls setLocation on undo/redo', () => {
    let wrapper = mount(MyComponent, {
      store,
      localVue,
      methods
    })

    store.dispatch('doUndo')
    expect(wrapper.vm.setLocation).toHaveBeenCalled()
  })
})

이것이 좋은 방법인지는 모르겠지만 실제 스토어와 로컬 Vue 인스턴스를 사용하고 있습니다.

조롱된 방법을 검증하려면 실제 모의 변수 자체를 사용합니다(visored variable을 사용하지 않음).wrapper):

expect(methods.setLocation).toHaveBeenCalled()

편집 확인 조롱된 메서드가 Vue에서 호출됩니다.

언급URL : https://stackoverflow.com/questions/55744295/how-to-mock-a-component-method

반응형