programing

Vuex에서 AssertionError를 {개체(유형, 텍스트)}과(와) 동일하게 던지는 단위 테스트

prostudy 2022. 7. 21. 23:54
반응형

Vuex에서 AssertionError를 {개체(유형, 텍스트)}과(와) 동일하게 던지는 단위 테스트

지금까지 유닛의 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) }...내가 기대했던 것을 제공하지 않는가?text와 함께expectedString맞습니까?이 문제에 대한 어떤 지침이라도 주시면 대단히 감사하겠습니다.

(문서 권장사항에 따라 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)
 })
})

언급URL : https://stackoverflow.com/questions/42863283/unit-testing-in-vuex-throwing-assertionerror-to-equal-object-type-text

반응형