在js文件中模拟Vuex吸气剂-TypeError:无法设置仅具有吸气剂的#<Object>的selectedSelectedCar属性

时间:2019-06-09 14:23:23

标签: vue.js jestjs vuex

我正在使用Vuex吸气剂向我返回一个属性,并测试该吸气剂工作正常。 现在,我想在js文件中调用此getter以在其函数中使用返回值,但是如果我在该getter上设置了模拟程序,则总是会遇到此错误: “ TypeError:无法设置#个仅具有吸气剂的属性selectedCar”

我还尝试用store.replaceState替换状态,并使用带有返回“测试”结果的“ mockImplementation”的间谍,但是它告诉我: “无法监视selectedCar属性,因为它不是函数;而是使用字符串”

import { store } from "../../store/store";

const helpers = {
    getArray() {
        const selectedCar = store.getters.selectedCar;
    }
};

export default helpers;

测试:

import helpers from "../helpers";
import { store } from "../../../store/store";

describe("getArray", () => {
    test("should get currentCar from store", () => {
        store.getters.selectedCar = jest.fn(() => "test");

        helpers.getArray();

        expect(store.getters.selectedCar).toBeCalled();
    });
});

商店:

import Vue from "vue";
import Vuex from "vuex";
import mutations from "./mutations";
import getters from "./getters";

Vue.use(Vuex);

export const store = new Vuex.Store({
    state: {
        sessionData: null,
    },
    mutations,
    getters
});

信件:

export default {
    selectedCar: (state) => {
        // const car = logic to retrieve the selectedCar
        return car;
    }
};

0 个答案:

没有答案