最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to mock a component method? - Stack Overflow

programmeradmin1浏览0评论

I'm simply trying to find out if a ponent method has been called after a store action, but I'm getting this error:

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

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

This is my unit test:

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()
  })
})

Not sure if this is a good practice or not, but I'm using the actual store and a local Vue instance.

I'm simply trying to find out if a ponent method has been called after a store action, but I'm getting this error:

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

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

This is my unit test:

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()
  })
})

Not sure if this is a good practice or not, but I'm using the actual store and a local Vue instance.

Share Improve this question edited Apr 19, 2019 at 8:21 tony19 139k23 gold badges277 silver badges347 bronze badges asked Apr 18, 2019 at 10:39 Nikolay DyankovNikolay Dyankov 7,24411 gold badges65 silver badges89 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

To verify the mocked method, use the actual mock variable itself (not via wrapper):

expect(methods.setLocation).toHaveBeenCalled()

发布评论

评论列表(0)

  1. 暂无评论