Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在vue测试中更改vuex getter值?_Javascript_Vue.js_Jestjs_Vue Test Utils - Fatal编程技术网

Javascript 如何在vue测试中更改vuex getter值?

Javascript 如何在vue测试中更改vuex getter值?,javascript,vue.js,jestjs,vue-test-utils,Javascript,Vue.js,Jestjs,Vue Test Utils,我有一个简单的测试: describe('App', () => { let store; beforeEach(() => { store = new Vuex.Store({ modules: { auth: { namespaced: true, getters: {

我有一个简单的测试:

describe('App', () => {
    let store;
    beforeEach(() => {
        store = new Vuex.Store({
            modules: {
                auth: {
                    namespaced: true,
                    getters: {
                        isLoggedIn: () => true,
                    }
                }
            }
        });
    });

    test('App hides navbar when user is logged in', () => {
        const wrapper = shallowMount(App, { store, localVue });
        expect(wrapper.contains(Nav)).toBe(false);
// store.getters['auth/isLoggedIn'] = () => false; // Not possible
    });
});
但我无法更改getter值,我在控制台中看到此错误:

TypeError:无法设置#的属性auth/isLoggedIn,该属性只有一个getter


如何在测试中设置getter值?

根据

  • 从存储导入getter。e、 g
  • 从“../../src/components/getter”导入getter

  • 放置getter而不是App
  • const wrapper=shallowMount(应用程序,{store,localVue})


    希望这有帮助。

    请参阅下面提到的代码。这就是修改getter值的方法

    let oldValue = store.getters['auth/isLoggedIn']
    console.log('oldValue', oldValue) // oldValue = true
    
    expect(store.getters['auth/isLoggedIn']).toBe(true)
    
      store = {
        getters: {
          'auth/isLoggedIn': !oldValue
        }
      }
    
      let newValue = store.getters['auth/isLoggedIn']
      console.log('newValue', newValue)  //newValue = false
    
      expect(store.getters['auth/isLoggedIn']).toBe(false)