IT이야기

시논j가 있는 스터빙부룩스 게터

cyworld 2022. 4. 17. 17:16
반응형

시논j가 있는 스터빙부룩스 게터

내 애플리케이션에서 라우터에 의해 사용되는 내비게이션 가드 안에 인증 상태를 확인할 수 있는 vuex 이름이 미리 지정된 게이터가 있다.게이터는 사용자가 인증되었는지 마법의 언더레이잉 검사를 한다.

인증된 상태에 따라 리디렉션이 수행되는지 확인하는 간단한 단위 테스트를 작성하고자 한다.나는 게이터를 터벅터벅 걷는 것에 매달렸다.

나의 getter는 다음과 같다.

isAuthenticated (state) {
  return state.token !== null
}

내 인증 모듈은 다음과 같다.

export default {
    namespaced: true,
    state,
    getters
}

그리고 내 가게는 다음과 같다.

export default new Vuex.Store({
    modules: {
        authentication
     }
})

내 탐색 가드는:

import store from '@/store'

export default (to, from, next) => {
  if (store.getters['authentication/isAuthenticated']) {
    next()
    return
  }

  next({name: 'login'})
}

나는 그 단위 시험을 작성했다.

   describe('authenticated-guard.spec.js', () => {
      let authenticatedStub
      beforeEach(() => {
        authenticatedStub = sandbox.stub(store.getters, 'authentication/isAuthenticated')
      })

      afterEach(() => {
        sandbox.restore()
      })

      it('should redirect to login route when the user is not authenticated', () => {
        // Given
        const to = {}
        const from = {}
        const next = spy()
        authenticatedStub.value(false)

        // When
        authenticatedGuard(to, from, next)

        // Then
        assert.ok(next.calledWith({name: 'login'}), 'should have redirected to login route')
      })
    })

장치 테스트는 다음 오류를 트리거한다.TypeError: Cannot redefine property: authentication/isAuthenticated.

나는 스텁의 대안으로 사용해왔다.authenticatedStub.value(false)하지만 오류는 똑같다.나는 경비원 시험에서 가게 로직을 따는 것을 피하기 위해 게이터를 찌를 수 없다.

소변을 보는 사람이 구성 요소 외부에 있는 게터를 터벅터벅 할 수 있는가?

안부 전해요

문제는 vuex가 getter를 구성할 수 없는 속성으로 설정하기 때문에 getter를 변경할 수 없다는 것이다.

그것들을 뭉치는 방법은 그것을 뭉치는 것이다.getters테스트가 다음과 같이 작동할 수 있도록 객체 자체를 선택하십시오.

describe('authenticated-guard.spec.js', () => {
  it('should redirect to', () => {
    const authenticatedStub = sandbox.stub(store, 'getters')
    // Given
    const to = {}
    const from = {}
    const next = spy()
    authenticatedStub.value({
      'authentication/isAuthenticated': false
    })

    // When
    authenticatedGuard(to, from, next)

    // Then
    expect(next.lastCall.args).to.deep.equal([{name: 'login'}], 'login route when the user is not authenticated')

    authenticatedStub.value({
      'authentication/isAuthenticated': true
    })

    authenticatedGuard(to, from, next)

    expect(next.lastCall.args).to.deep.equal([], 'next route when the user is authenticated')
  })
})

참조URL: https://stackoverflow.com/questions/46771146/stubbing-vuex-getters-with-sinonjs

반응형