unit-testing - Vuex 中的单元测试抛出 AssertionError 等于 { Object (type, text) }

标签 unit-testing vue.js mocha.js chai vuex

到目前为止,对 Vuex 操作进行单元测试一直是一场噩梦,我似乎无法理解它实际上在寻找什么。我正在关注 https://vuex.vuejs.org/en/testing.html并使用他们的 Action 辅助函数。

我有一个将文本添加到按钮的简单操作。

createButton({ commit, state }, payload) {
    let btnString = '';

    if (payload.hasCookie) {
        btnString = 'Welcome back';
    } else if (payload.isLoggedIn) {
        btnString = 'Sign out';
    } else {
        btnString = 'Sign up';
    }

    commit({ type: 'CREATE_BUTTON_TEXT', text: btnText });
},

在我的测试中...

describe('Create Button Text', () => {
        it('should render the buttons string ', (done) => {
            const expectedString = 'Welcome back';
            const mock = {
                hasCookie: true,
                isLoggedIn: false,
            };

            testAction(actions.createButton, mock, state, [
                { type: 'CREATE_BUTTON_TEXT', text: expectedString },
            ], done);
        });
    });

它正在返回 AssertionError: expected 'CREATE_BUTTON_TEXT' to equal { Object (type, text) }... 我没有提供预期的 textexpectedString 正确吗?非常感谢有关此事的任何指导。

(根据文档中的建议使用 Mocha + Chai)。

最佳答案

看来您的提交语法有误。如果您使用了 testAction 帮助器,则参数 expectedMutations 需要一个具有类型和负载的对象列表。 以下代码对我有用:

createButton({ commit, state }, payload) {
  let btnString = ''

  if (payload.hasCookie) {
    btnString = 'Welcome back'
  } else if (payload.isLoggedIn) {
    btnString = 'Sign out'
  } else {
    btnString = 'Sign up'
  }

  commit('CREATE_BUTTON_TEXT', btnString)
}

及测试方法:

describe('Create Button Text', () => {
 it('should render the buttons string ', (done) => {
  const expectedString = 'Welcome back'
  const mock = {
   hasCookie: true,
   isLoggedIn: false
  }

  testAction(actions.createButton, mock, {}, [
   { type: 'CREATE_BUTTON_TEXT', payload: expectedString }
  ], done)
 })
})

关于unit-testing - Vuex 中的单元测试抛出 AssertionError 等于 { Object (type, text) },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42863283/

相关文章:

unit-testing - 为什么在 goroutine 中声明时 benbjohnson/clock 模拟计时器不执行?

c# - 使用最小起订量 : mock object throwing 'TargetParameterCountException'

用于重试失败调用和取消先前调用的 javascript 模式

jquery - vuejs : recommended way to use a jquery plugin

node.js - 使用来自 WebPack.DefinePlugin 的全局注入(inject)变量进行 NodeJS Mocha 单元测试

java - 启动MiniDFSCluster时出错

java - 一个方法在 JUnit 中生成了预期的结果,但它说它是 null

docker - dockerized 环境中的 Nuxt SSR 水合

node.js - 如何使用mocha、node和selenium集成测试react-select多选下拉菜单?

javascript - Mocha 与 Vue : Resolution method is overspecified