【问题标题】:Unit testing in Vuex throwing AssertionError to equal { Object (type, text) }Vuex 中的单元测试抛出 AssertionError 等于 { Object (type, text) }
【发布时间】:2017-08-09 08:25:51
【问题描述】:

到目前为止,单元测试 Vuex 动作一直是一场噩梦,我似乎无法理解它真正在寻找什么。我正在关注https://vuex.vuejs.org/en/testing.html 并使用他们的操作助手功能。

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

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)。

【问题讨论】:

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


    【解决方案1】:

    看来你的提交语法是错误的。如果您使用了 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)
     })
    })
    

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多