我基本上有一个存储,依赖于另一个存储,我没有看到一种方法,只模仿依赖存储.示例伪代码vue 3 ish:
// the store I want to mock
export const useStore1 = defineStore({
id: 'store1',
state: (): State => ({
someName:'blarg', // I know this is static but lets pretend it can change.
}),
// getter I want to mock
getters: {
name: (state) => state.someName,
}
}
// store I want to test
export const useStoreTwo = defineStore({
id: 'store2',
state: (): State => ({
someValue:'bar'
}),
getters: {
value: (state) => {
const store1 = useStore1() // dependency here
return `${store1.name} state.someValue`
},
}
}
test
it('should return something' () => {
//****
someplace I would mock useStateOne and have it return
a stub store with the getter name that returns 'foo'
***//
const store2 = useStoreTwo();
expect(store2.value).toBe('foo bar');
})
1条答案
按热度按时间f1tvaqid1#
这对你很有帮助。
https://pinia.vuejs.org/cookbook/testing.html#mocking-getters
默认情况下,将像常规用法一样计算任何getter**,但您可以通过将getter设置为任何所需的值来手动强制指定值**