vuejs namespace getters undefined error

时间:2017-11-14 22:57:18

标签: vuejs2 vuex

我在测试命名空间的getter时遇到了困难,虽然它有效但我无法成功测试它。

getContextMenuListSelectedState: function (menu) {
  return this.$store.getters['appContext/getContextMenuListSelectedState'](menu)
}

<view-switcher @switchView="setCurrentView" />

setCurrentView: function (obj) {
...
let maintSelected = this.getContextMenuListSelectedState('docsmaint').label === 'maint'
...
}

is throwing an error in test:
'[Vue warn]: Error in event handler for "switchView": "TypeError: this.$store.getters**.appContext/getContextMenuListSelectedState** is not a function"

1 个答案:

答案 0 :(得分:0)

该错误非常具有误导性。我终于能够用假的存根了。按照上面的规定重构后。

 function fakeGetter (menu) {
      return store.state.appContext.contextMenusLists[menu].filter((item) => {
        return item.selected
      })
    }

getContextMenuListSelectedStateSpy = sinon.stub(omsLayout.methods, 'getContextMenuListSelectedState').callsFake(fakeGetter)