Jestjs 测试是否使用特定参数调用Vuex操作(来自组件)
我正在为Vue组件的方法编写一个Jest测试。具体地说,我想测试是否使用某个参数调用了Vuex操作(我已使用Jestjs 测试是否使用特定参数调用Vuex操作(来自组件),jestjs,vue-component,vuex,Jestjs,Vue Component,Vuex,我正在为Vue组件的方法编写一个Jest测试。具体地说,我想测试是否使用某个参数调用了Vuex操作(我已使用mapActions映射到组件中的此操作) 我模拟了应该传递给动作的参数——我的测试接近尾声,但动作收到的参数有一堆额外的信息,我不知道为什么 我正在测试的组件中的方法是: triggerSave (trip) { if (this.canSave) { const tripToSave = { tripInProcess: true, trip: { tripId: trip
mapActions
映射到组件中的此操作)
我模拟了应该传递给动作的参数——我的测试接近尾声,但动作收到的参数有一堆额外的信息,我不知道为什么
我正在测试的组件中的方法是:
triggerSave (trip) {
if (this.canSave) {
const tripToSave = { tripInProcess: true, trip: { tripId: trip.tripId, }, };
this.updateMyTrips(tripToSave);
}
},
其中,updateMyTrips
是我在组件中映射到的Vuex操作
在我的jest测试中,我模拟了我在每个(以及存储)之前在中测试的动作
我的测试本身是
test('trigger save calls updateMyTrips action', () => {
const mockTrip = [
{
tripId: 1234,
tripDestination: 'Rio di Janeiro',
},
];
const wrapper = shallowMount(TripPlanner, { store, localVue, propsData: { trip: mockTrip, canSave: true, }, },);
wrapper.vm.updateMyTrips(mockTrip[0]);
const mockUpdate = { tripInProcess: true, trip: { tripId: mockTrip.tripId }, };
expect(actions.updateMyTrips).toHaveBeenCalledWith(mockUpdate);
});
当我运行测试并查看预期/接收的值时,它确实会接收我的mockUpdate
对象,但也会接收
"commit": [Function anonymous],
+ "dispatch": [Function anonymous],
+ "getters": Object {},
+ "rootGetters": Object {},
+ "rootState": Object {
+ "addOns": Object {},
+ "state": Object {},
所以测试失败了
如果我只是测试expect(actions.updateMyTrips).toHaveBeenCalled
测试通过了…但是当我测试它是否被该特定参数调用时,它失败了
我不明白为什么商店的发送、获取、状态等也被接收 第一个参数是pass automatic,第二个参数是要传递给vuex的数据
您可以像第一个参数一样传递:
expect.any(Object),
第二个参数是你的数据
const mockTrip = [
{
tripId: 1234,
tripDestination: 'Rio di Janeiro',
},
];
我不理解文档中关于这个问题的内容。因为这就是操作接收的上下文对象的样子<代码>vm.updateMyTrips!==actions.updateMyTrips
。啊,这是有道理的……这个测试应该是调用了这个操作。非常感谢。
const mockTrip = [
{
tripId: 1234,
tripDestination: 'Rio di Janeiro',
},
];